var testUrl1 = 'https://www.youtube.com/watch?v=qyCtwmlz1Dw';
var testUrl2 = 'http://www.amazon.it/s/ref=nb_sb_noss_2?&url=search-alias%3Daps&field-keywords=arduino';
//parameter ok but without value
var testUrl3 = 'https://www.youtube.com/watch?v=';
//multiple quesiton ? chars
var testUrl4 = 'http://www.amazon.it/s/ref=nb_sb_noss_2?&url=search-alias?Daps&field-keywords=arduino';
//search for parameter 'v' and 'field-keywords' in url
$('#result').append(getUrlParameterValue(testUrl1, 'v') + '<br/>');
$('#result').append(getUrlParameterValue(testUrl2, 'field-keywords') + '<br/>');
//search somethings not exist
$('#result').append(getUrlParameterValue(testUrl1, 'fake-parameter') + '<br/>');
$('#result').append(getUrlParameterValue(testUrl1, 'other-fake') + '<br/>');
$('#result').append(getUrlParameterValue(testUrl3, 'v') + '<br/>');
$('#result').append(getUrlParameterValue(testUrl4, 'url') + '<br/>');
function getUrlParameterValue(url, parameter) {
var questionSplit = url.split('?');
//prevent multiple split if more then one quesiton (?) char
questionSplit.shift();
var onlyParameters = questionSplit.join('?');
var splittedParameters = onlyParameters.split('&');
var found = false;
var value = null;
for (var c = 0; c < splittedParameters.length; c++) {
var parts = splittedParameters[c].split('=');
if (parts[0] == parameter) {
value = parts[1];
if ($.trim(value) == '') {
found = false;
} else {
found = true;
}
}
if (found) {
return value;
}
}
if (!found) {
return false;
}
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.