javascript 使用 jquery 更改 URL 参数或删除 URL 的某些部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18841516/
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
Change URL params or remove some part of URL with jquery
提问by dev1234
I have this URL and wanting to know how can I remove this section from it via a jQuery event.
我有这个 URL,想知道如何通过 jQuery 事件从中删除此部分。
Need to remove:
需要删除:
&activities_id=13&session_id=14&back=1
&activities_id=13&session_id=14&back=1
Original URL:
原网址:
EDIT
编辑
Sorry i think i havent included the most important section. I should change the Address BAR url not a normal string.
抱歉,我想我没有包括最重要的部分。我应该更改地址栏网址而不是普通字符串。
for example, if i have this url in the address bar - http://somedomain.com/ijob-css/index.php/after change, address bar should contain http://somedomain.com/xxx=111, without page refreshing.
例如,如果我在地址栏中有这个 url - http://somedomain.com/ijob-css/index.php/更改后,地址栏应该包含http://somedomain.com/xxx=111,没有页面令人耳目一新。
回答by Sanjeevi Rajagopalan
Do you mean you want the URL without the query parameter part? If then see if this helps.
你的意思是你想要没有查询参数部分的 URL?如果然后看看这是否有帮助。
var test = 'http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=&activities_id=13&session_id=14&back=1';
alert(test.substring(0, test.indexOf('?')));
If you want until first query parameter name then just seek until index of &
如果您想要直到第一个查询参数名称,则只需查找 & 的索引
Update :
更新 :
If you are using HTML5 then what you ask is possible. Check browser history manipulation. You can find details about this here.
如果您使用的是 HTML5,那么您的要求是可能的。检查浏览器历史操作。您可以在此处找到有关此内容的详细信息。
I believe replaceState() is the answer for your problem. However it is not supported in all browsers/versions. History.js wraps HTML5 state features and provides additional support for HTML4 browsers.
我相信 replaceState() 是您问题的答案。但是,并非所有浏览器/版本都支持它。History.js 包装了 HTML5 状态功能并为 HTML4 浏览器提供了额外的支持。
回答by Trevor Dixon
var lastPart = 'query=';
var url = 'http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=&activities_id=13&session_id=14&back=1'.split(lastPart)[0] + lastPart;
回答by Alex Art.
var index = original_url.indexOf("=");
var new_url = original_url.substring(0,index+1);
回答by andrewb
See below.
见下文。
var positionToSubstring = this.location.href.indexOf('&');
var newURI = this.location.href.substring(0, positionToSubstring);
回答by davidkonrad
use this
用这个
var test='http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=&activities_id=13&session_id=14&back=1';
test=test.split('&')[0];
console.log(test);
outputs
输出
http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=
http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=
回答by Shareek Ahamed
Try this out
试试这个
var new_url = old_url.substring(0, old_url.indexOf('&'));
Example:http://jsfiddle.net/SjrqF/
示例:http : //jsfiddle.net/SjrqF/
var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';
url = url.slice( 0, url.indexOf('&') );
or:
或者:
Example:http://jsfiddle.net/SjrqF/1/
示例:http : //jsfiddle.net/SjrqF/1/
var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';
url = url.split( '&' )[0];