mheavers mheavers - 1 year ago 121
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, but what the api generates is 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 Source


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

Modified: this will remove all parameters or fragments from url

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