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
<
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:-)
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);
}
Hmm... Looking for better way... here it is
var onlyUrl = window.location.href.replace(window.location.search,'');
This worked for me:
window.location.replace(window.location.pathname)
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];
}
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));