admin管理员组文章数量:1277502
I have the following url:
url = ''
How would I get the url without the querystring? Right, now I am doing the following, but is there a single method to do this instead of adding two?
window.location.origin + window.location.pathname
I have the following url:
url = 'http://stackoverflow./questions/ask?new=question'
How would I get the url without the querystring? Right, now I am doing the following, but is there a single method to do this instead of adding two?
window.location.origin + window.location.pathname
Share
Improve this question
asked Oct 16, 2014 at 22:54
David542David542
110k205 gold badges566 silver badges1k bronze badges
2 Answers
Reset to default 6To get all the different parts of a url, location.protocol + '//' + location.host + location.pathname
would be the correct syntax. Here's an example displaying the url where this snippet is hosted:
document.body.innerHTML = "The snippet is at this web address: " + getURL();
function getURL() {
return location.protocol + '//' + location.host + location.pathname
}
For example like this - location.href.split("?")[0]
- split by ?
and take the first element of the resultant array. It will work even if there is no ?
in location - the whole url will be the single element of array.
ps: downvoter - ments? don't be a chicken.
本文标签: Javascript get url without querystringStack Overflow
版权声明:本文标题:Javascript get url without querystring - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741285887a2370267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论