使用 javascript 或 jquery 删除 url 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4651990/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:28:09  来源:igfitidea点击:

remove url parameters with javascript or jquery

javascriptjqueryquery-stringexpression

提问by mheavers

I am trying to use the youtube data api to generate a video playlist.

我正在尝试使用 youtube 数据 api 生成视频播放列表。

However, the video urls require a format of:

但是,视频网址需要以下格式:

youtube.com/watch?v=3sZOD3xKL0Y

but what the api generates is:

但是 api 生成的是:

youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata

So what I need to do is be able to select everything after and including the ampersand(&) and remove it from the url.

所以我需要做的是能够选择包括&符号(&)之后的所有内容并将其从网址中删除。

Any way to do this with javascript and some sort of regular expression?

有什么办法可以用javascript和某种正则表达式来做到这一点?

采纳答案by Jacob Relkin

Simple:

简单的:

var new_url = old_url.substring(0, old_url.indexOf('?'));

Modified: this will remove all parameters or fragments from url

修改:这将从 url 中删除所有参数或片段

var oldURL = [YOUR_URL_TO_REMOVE_PARAMS]
var index = 0;
var newURL = oldURL;
index = oldURL.indexOf('?');
if(index == -1){
    index = oldURL.indexOf('#');
}
if(index != -1){
    newURL = oldURL.substring(0, index);
}

回答by goldylucks

What am I missing?

我错过了什么?

Why not:

为什么不:

url.split('?')[0] 

回答by hriziya

Hmm... Looking for better way... here it is

嗯......寻找更好的方法......在这里

var onlyUrl = window.location.href.replace(window.location.search,'');

回答by user113716

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];

回答by chozha rajan

//user113716 code is working but i altered as below. it will work if your URL contain "?" mark or not
//replace URL in browser
if(window.location.href.indexOf("?") > -1) {
    var newUrl = refineUrl();
    window.history.pushState("object or string", "Title", "/"+newUrl );
}

function refineUrl()
{
    //get full url
    var url = window.location.href;
    //get url after/  
    var value = url = url.slice( 0, url.indexOf('?') );
    //get the part after before ?
    value  = value.replace('@System.Web.Configuration.WebConfigurationManager.AppSettings["BaseURL"]','');  
    return value;     
}

回答by sidanmor

Use this function:

使用这个功能:

var getCleanUrl = function(url) {
  return url.replace(/#.*$/, '').replace(/\?.*$/, '');
};

// get rid of hash and params
console.log(getCleanUrl('https://sidanmor.com/?firstname=idan&lastname=mor'));

If you want all the href parts, use this:

如果您想要所有 href 部分,请使用以下命令:

var url = document.createElement('a');
url.href = 'https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container';

console.log(url.href); // https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container
console.log(url.protocol); // https:
console.log(url.host); // developer.mozilla.org
console.log(url.hostname); // developer.mozilla.org
console.log(url.port); // (blank - https assumes port 443)
console.log(url.pathname); // /en-US/search
console.log(url.search); // ?q=URL
console.log(url.hash); // #search-results-close-container
console.log(url.origin); // https://developer.mozilla.org

回答by sfratini

Well, I am using this:

嗯,我正在使用这个:

stripUrl(urlToStrip){
        let stripped = urlToStrip.split('?')[0];
        stripped = stripped.split('&')[0];
        stripped = stripped.split('#')[0];
        return stripped;
    }

or:

或者:

    stripUrl(urlToStrip){
        return urlToStrip.split('?')[0].split('&')[0].split('#')[0];
    }

回答by Christian Joudrey

You could use a RegEx to match the value of vand build the URL yourself since you know the URL is youtube.com/watch?v=...

您可以使用 RegEx 来匹配值v并自己构建 URL,因为您知道 URL 是 youtube.com/watch?v=...

http://jsfiddle.net/akURz/

http://jsfiddle.net/akURz/

var url = 'http://youtube.com/watch?v=3sZOD3xKL0Y';
alert(url.match(/v\=([a-z0-9]+)/i));

回答by Santiago Martí Olbrich

This worked for me:

这对我有用:

window.location.replace(window.location.pathname)

回答by Adrian

For example we have:

例如我们有:

example.com/list/search?q=Somethink

And you need use variable url like this by window.location.href:

并且您需要通过window.location.href使用这样的变量 url :

example.com/list/edit

From url:

来自网址:

example.com/list/search?q=Somethink
example.com/list/
var url = (window.location.href);
    url = url.split('/search')[0];
    url = (url + '/edit');

This is simple solution:-)

这是一个简单的解决方案:-)