使用 jQuery 或 Javascript 拆分 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20190054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
splitting the URL using jQuery or Javascript
提问by Billy
URL:
网址:
http://testwebsite.com/page1.aspx#page/1
I want to split the URL upto aspx using jQuery or Javascript, meant I want to split the URL so that it should be:
我想使用 jQuery 或 Javascript 将 URL 拆分为 aspx,这意味着我想拆分 URL,以便它应该是:
http://testwebsite.com/page1.aspx
I have tried this code;
我试过这段代码;
var url=document.URL;
var arr=url.split('#');
var myoutput=arr[0];
Is it the right way to split? Is there any other way which is reliable and better?
这是正确的拆分方式吗?有没有其他可靠和更好的方法?
回答by Grant Thomas
You could just use location.host + location.pathname
to generate the specific page path instead of trying to split down a string to contain only what you want it to.
您可以只使用location.host + location.pathname
生成特定的页面路径,而不是尝试拆分字符串以仅包含您想要的内容。
回答by Hiral
回答by Trendy
If your intended goal is to grab the URL up to the point of the .aspx extension, I would suggest splitting the URL at the extension and then appending it to the split. This would account for other symbols that could possibly precede the extension:
如果您的预期目标是获取 .aspx 扩展名之前的 URL,我建议在扩展名处拆分 URL,然后将其附加到拆分中。这将解释可能在扩展之前的其他符号:
var url = document.URL,
arr = url.split('.aspx'),
myOutput = arr[0] + '.aspx';