mheavers mheavers - 6 months ago 16
Javascript Question

remove url parameters with javascript or jquery

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?

Answer

Simple:

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

Modified: this will remove all parameters or fragments from 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);
}
Comments