开发者

window.location.hash not working in Chrome/Safari

I have the following code on my website, which basically checks to see if there is a hash in the URL, and if there is then it triggers a click on a tab. This works fine apart from in Chrome/Safari - any ideas how I can fix this?

jQuery(document).ready(function() {
    if(window.location.hash){ 
        $("a#viewapart").trigger('click');
    }
});开发者_运维百科

It doesn't work if I substitute alert('hello'); so it is just not recognizing if(window.location.hash) for some reason.

Thanks!


You're likely executing it before the carousel script has been initialized and bound all clicks.

$(document).ready(function() {
    if (window.location.hash){ 
        $("a#viewapart").trigger('click');
    }
    $("#slider").jcarousel();
});

You need to execute it after the carousel script is been initialized.

$(document).ready(function() {
    $("#slider").jcarousel();
    if (window.location.hash){ 
        $("a#viewapart").trigger('click');
    }
});


Try like this:

if (window.location.hash != null && window.location.hash.length > 0) {
    $('a#viewapart').trigger('click');
}


Should work, unless you are setting the hash dynamically.

jQuery(document).ready(function() {
    var t = window.location;
    var hash = t.hash || ((t = t.href.match(/#([^?]*)/)) && t[1]);
    if(hash){ 
        $("a#viewapart").trigger('click');
    }
});


If you try to put a SCRIPT tag just before the </BODY> tag instead?

  ...
  <script>
    if(window.location.hash){ 
      $("a#viewapart").trigger('click');
    }
  </script>
</body>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜