Delali
Delali

Reputation: 908

Do something if location.hash is equal to some value in jquery

I have a function called Designer and I want to return this function if location.hash equals isDesign. I tried the following...

function Designer() {
  $("#boxA").fadeIn(700);
  $("#boxA span#closeControl").on("click",function(){
   $("#builderBox").fadeOut(700);
  });
}
if (location.hash = "isDesign") {
return Designer();
}

Instead of the function executing, it assigns the isDesign hash to the url and reloads the page several times. Please, what should I do to correct this. All answers will be appreciated. Thank you.

Upvotes: 1

Views: 8464

Answers (2)

Esko
Esko

Reputation: 4207

Try this:

$(function() {
    if (location.hash === "#isDesign") {
        Designer();
    }
});

function Designer() {
    $("#boxA").fadeIn(700);
    $("#boxA span#closeControl").on("click",function(){
       $("#builderBox").fadeOut(700);
    });
}

or just

$(function() {
    if (location.hash === "#isDesign") {
        $("#boxA").fadeIn(700);
        $("#boxA span#closeControl").on("click",function(){
           $("#builderBox").fadeOut(700);
        });
    }
});

Upvotes: 5

Peter van der Wal
Peter van der Wal

Reputation: 11856

Use

if (location.hash == "isDesign") {
    return Designer();
}

Single = is assignment, double == is comparison

Upvotes: 0

Related Questions