相关疑难解决方法(0)

Javascript/jQuery获取网站的根URL

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

该网站的根目录是:" http://www.example.com/folder1/mywebsite "

我想动态地获得"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完成,它也会很好.

html javascript jquery

18
推荐指数
2
解决办法
4万
查看次数

标签 统计

html ×1

javascript ×1

jquery ×1