gpt4 book ai didi

Javascript/jQuery 获取网站的根 url

转载 作者:技术小花猫 更新时间:2023-10-29 12:10:13 25 4
gpt4 key购买 nike

我有网站:"http://www.example.com/folder1/mywebsite/subfolder/page.html "

站点的根目录是:“http://www.example.com/folder1/mywebsite

我想动态获取“我的网站”的根网址。如果我将这个网站发布到另一个地方,那么我将不需要更改脚本。

例如:“http://www.example.com/otherFolder/test/myChangedWebsite/subfolder/page.html

我会得到:"http://www.example.com/otherFolder/test/myChangedWebsite "

我在“page.html”javascript 中试过:var url = window.location.protocol + "//"+ window.location.host + '/otherPage.html

但这只给我“http://www.example.com/otherPage.html”。

我也知道 location.origin 但这不是所有浏览器都支持的,因为我在链接上发现:http://www.w3schools.com/jsref/prop_loc_origin.asp

如果能用jQuery来做就好了。

最佳答案

这是一个与 Hawk 上面的帖子相同的功能,只是短得多:

function getBaseUrl() {
var re = new RegExp(/^.*\//);
return re.exec(window.location.href);
}

详情在这里:Javascript: Get base URL or root URL

关于Javascript/jQuery 获取网站的根 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23425909/

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