gpt4 book ai didi

Javascript获取没有查询字符串的url

转载 作者:行者123 更新时间:2023-12-01 11:10:23 27 4
gpt4 key购买 nike

我有以下网址:

url = 'http://stackoverflow.com/questions/ask?new=question'

如果没有查询字符串,我将如何获取 url?是的,现在我正在执行以下操作,但是有没有一种方法可以做到这一点而不是添加两个方法?
window.location.origin + window.location.pathname

最佳答案

要获取 url 的所有不同部分,location.protocol + '//' + location.host + location.pathname将是正确的语法。这是显示托管此代码段的网址的示例:

document.body.innerHTML = "The snippet is at this web address: " + getURL();

function getURL() {
return location.protocol + '//' + location.host + location.pathname
}

关于Javascript获取没有查询字符串的url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26415222/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com