Search code examples
jqueryurllocationhrefattr

Check url stored in variable to current url


I can't find out why this is not working:

var myurl = "http://domain.com";
var currenturl = $(location).attr('href');
if (myurl == currenturl) {
...

Both myurl and currenturl are the same, yet the script in the if statement is not get executed.

EDIT:

$(document).ready(function(){
var myurl = "http://domain.com";
var currenturl = window.location.href;
//alert(currenturl);
    if (myurl === currenturl) {
        alert('should see this');
    } else {
    }
});

SOLUTION:

$(document).ready(function(){
var myurl = "http://domain.com/"; // see the slash at the end
    if (myurl == location.href) {
        alert('that ss the same page');
    } else {
    }
});

Any suggestion? Thans.


Solution

  • You don't have to use jQuery for it, just:

    if (myurl == location.href) {
        // the same
    }
    

    If that doesn't work, make sure they're both the same by debugging their values:

    console.log(myurl, location.href);