【发布时间】:2013-08-15 21:46:47
【问题描述】:
如果我当前的页面是这种格式...
http://www.mydomain.com/folder/mypage.php?param=value
有没有简单的方法来获得这个
http://www.mydomain.com/folder/mypage.php
使用javascript?
【问题讨论】:
标签: javascript jquery url
如果我当前的页面是这种格式...
http://www.mydomain.com/folder/mypage.php?param=value
有没有简单的方法来获得这个
http://www.mydomain.com/folder/mypage.php
使用javascript?
【问题讨论】:
标签: javascript jquery url
尽量用split点赞
var url = "http://www.mydomain.com/folder/mypage.php?param=value";
var url_array = url.split("?");
alert(url_array[0]); //Alerts http://www.mydomain.com/folder/mypage.php
即使GET 中有很多参数,第一段将是没有GET 参数的URL。
这是DEMO
【讨论】:
http://www.domain.com/page#anchor?parameter ?使用文档对象及其 api 是一个更强大的解决方案 IMO
【讨论】:
不要做这个正则表达式和拆分东西。使用浏览器内置的 URL 解析器。
window.location.origin + window.location.pathname
如果您需要解析不是当前页面的 URL:
var url = document.createElement('a');
url.href = "http://www.example.com/some/path?name=value#anchor";
console.log(url.origin + url.pathname);
并且支持IE(因为IE没有location.origin):
location.protocol + '//' + location.host + location.pathname;
【讨论】:
var options = decodeURIComponent(window.location.search.slice(1))
.split('&')
.reduce(function _reduce (/*Object*/ a, /*String*/ b) {
b = b.split('=');
a[b[0]] = b[1];
return a;
}, {});
【讨论】:
试试这个:
var url=document.location.href;
var mainurl=url.split("?");
alert(mainurl[0]);
【讨论】: