remove url parameters with javascript or jquery

JavascriptJqueryQuery StringExpression

Javascript Problem Overview


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

However, the video urls require a format of:

youtube.com/watch?v=3sZOD3xKL0Y

but what the api generates is:

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 Solutions


Solution 1 - Javascript

What am I missing?

Why not:

url.split('?')[0] 

Solution 2 - Javascript

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

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

Solution 3 - Javascript

Example: 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/

var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';

url = url.split( '&' )[0];

Solution 4 - Javascript

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:

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

Solution 5 - Javascript

//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;     
}

Solution 6 - Javascript

This worked for me:

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

Solution 7 - Javascript

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

http://jsfiddle.net/akURz/

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

Solution 8 - Javascript

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

Solution 9 - Javascript

For example we have:

example.com/list/search?q=Somethink

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

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:-)

Solution 10 - Javascript

> No splits.. :) The correct/foolproof way is to let the native browser BUILT-IN functions do the heavy lifting using urlParams, the heavy lifting is done for you.

//summary answer - this one line will correctly replace in all current browsers window.history.replaceState({}, '', ${location.pathname}?${params});

// 1 Get your URL 
let url = new URL('https://tykt.org?unicorn=1&printer=2&scanner=3');
console.log("URL: "+ url.toString());

// 2 get your params
let params = new URLSearchParams(url.search);
console.log("querys: " + params.toString());

// 3 Delete the printer param, Query string is now gone
params.delete('printer'); 
console.log("Printer Removed: " + params.toString()); 

// BELOW = Add it back to the URL, DONE!
___________

NOW Putting it all together in your live browser

// Above is a breakdown of how to get your params // 4 then you simply replace those in your current browser!! window.history.replaceState({}, '', ${location.pathname}?${params});

Sample working Javascript Fiddle here working sample code to replace query substring and query params

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmheaversView Question on Stackoverflow
Solution 1 - JavascriptgoldylucksView Answer on Stackoverflow
Solution 2 - JavascripthriziyaView Answer on Stackoverflow
Solution 3 - Javascriptuser113716View Answer on Stackoverflow
Solution 4 - JavascriptsidanmorView Answer on Stackoverflow
Solution 5 - Javascriptchozha rajanView Answer on Stackoverflow
Solution 6 - JavascriptSantiago Martí OlbrichView Answer on Stackoverflow
Solution 7 - JavascriptChristian JoudreyView Answer on Stackoverflow
Solution 8 - Javascriptsebastianf182View Answer on Stackoverflow
Solution 9 - JavascriptAdrianView Answer on Stackoverflow
Solution 10 - JavascriptTransformerView Answer on Stackoverflow