最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Do something if location.hash is equal to some value in jquery - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Dec 13, 2013 at 12:41 Delali asked Dec 13, 2013 at 12:40 DelaliDelali 9082 gold badges16 silver badges24 bronze badges 4
  • The Designer-function does not return anything, do you simply want to call it if the hash matches? Also the hash, I believe, would be "#isDesign" would it not? – Esko Commented Dec 13, 2013 at 12:43
  • Yes, but it is still not working. I tried to call an alert instead of returning the function but it did not work. – Delali Commented Dec 13, 2013 at 12:48
  • if (location.hash = "isDesign") You're missing (at least) an = on this line. A single = is assigning a value to location.hash. Since you're paring values, you need == (or === for strict typing). – fastasleep Commented Mar 14, 2014 at 7:53
  • The unsolved problem here is how to deal with URLencoding; e.g. if (location.hash == '#'+id) isn't quite right if e.g. id == "HQ9+", because you'll find that location.hash == "#HQ9%2B" instead. – Quuxplusone Commented Nov 12, 2023 at 21:46
Add a ment  | 

2 Answers 2

Reset to default 5

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);
        });
    }
});

Use

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

Single = is assignment, double == is parison

发布评论

评论列表(0)

  1. 暂无评论