简体   繁体   English

如何使用javascript获取没有$ _GET参数的url路径?

[英]How to get url path without $_GET parameters using javascript?

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?

Don't do this regex and splitting stuff. 不要做这个正则表达式和分裂的东西。 Use the browser's built-in URL parser. 使用浏览器的内置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://stackoverflow.com/a/6168370/711902 ) (来自https://stackoverflow.com/a/6168370/711902的启示)

Try to use split like 尝试使用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

Even we have many parameters in the GET , the first segment will be the URL without GET parameters. 即使我们在GET有许多参数, 第一个段将是没有GET参数的URL

This is DEMO 这是DEMO

try this: 试试这个:

var url=document.location.href;
var mainurl=url.split("?");
alert(mainurl[0]);

Try 尝试

var result = yourUrl.substring(0, yourUrl.indexOf('?'));

Working demo 工作演示

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;
   }, {});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM