If my current page is in this format...
如果我当前的页面是这种格式……
http://www.mydomain.com/folder/mypage.php?param=value
Is there an easy way to get this
有没有简单的方法来解决这个问题?
http://www.mydomain.com/folder/mypage.php
using javascript?
使用javascript ?
5 个解决方案
#1
6
Don't do this regex and splitting stuff. Use the browser's built-in URL parser.
不要做这个regex和分裂的事情。使用浏览器的内置URL解析器。
window.location.origin + window.location.pathname
And if you need to parse a URL that isn't the current page:
如果您需要解析一个不是当前页面的URL:
var url = document.createElement('a');
url.href = "http://www.example.com/some/path?name=value#anchor";
console.log(url.origin + url.pathname);
And to support IE (because IE doesn't have location.origin
):
支持IE(因为IE没有location.origin):
location.protocol + '//' + location.host + location.pathname;
(Inspiration from https://*.com/a/6168370/711902)
(灵感来自https://*.com/a/6168370/711902)
#2
3
Try to use split
like
尝试使用split like
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
Even we have many parameters in the GET
, the first segment will be the URL
without GET
parameters.
即使我们在GET中有很多参数,第一个段也是没有GET参数的URL。
This is DEMO
这是演示
#3
2
try this:
试试这个:
var url=document.location.href;
var mainurl=url.split("?");
alert(mainurl[0]);
#5
0
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;
}, {});
#1
6
Don't do this regex and splitting stuff. Use the browser's built-in URL parser.
不要做这个regex和分裂的事情。使用浏览器的内置URL解析器。
window.location.origin + window.location.pathname
And if you need to parse a URL that isn't the current page:
如果您需要解析一个不是当前页面的URL:
var url = document.createElement('a');
url.href = "http://www.example.com/some/path?name=value#anchor";
console.log(url.origin + url.pathname);
And to support IE (because IE doesn't have location.origin
):
支持IE(因为IE没有location.origin):
location.protocol + '//' + location.host + location.pathname;
(Inspiration from https://*.com/a/6168370/711902)
(灵感来自https://*.com/a/6168370/711902)
#2
3
Try to use split
like
尝试使用split like
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
Even we have many parameters in the GET
, the first segment will be the URL
without GET
parameters.
即使我们在GET中有很多参数,第一个段也是没有GET参数的URL。
This is DEMO
这是演示
#3
2
try this:
试试这个:
var url=document.location.href;
var mainurl=url.split("?");
alert(mainurl[0]);
#4
#5
0
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;
}, {});