Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery get url param if exists

Tags:

jquery

url

get

Im trying to construct a script that will check if a url variable called result exists and if it does then it checks if the value equals success or not.

I tried the following but if result doesn't exist then it errors my script as undefined

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}
var first = getUrlVars()["result"];
alert(first);
like image 545
ngplayground Avatar asked May 26 '26 02:05

ngplayground


1 Answers

And why don't you simply check if it is undefined ?

if (first !== undefined)
     alert(first);
else alert('ooops');

Or you probably can also define something like a 'default value':

var first = getUrlVars().result || '';
like image 113
arkascha Avatar answered May 27 '26 15:05

arkascha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!