Chris
Chris

Reputation: 1587

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!

Upvotes: 0

Views: 9504

Answers (4)

BalusC
BalusC

Reputation: 1108537

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

Upvotes: 1

Mic
Mic

Reputation: 25154

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>

Upvotes: 0

BGerrissen
BGerrissen

Reputation: 21680

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

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Try like this:

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

Upvotes: 0

Related Questions