views:

208

answers:

4

Hi all,

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.

URL: http://aonachmor.cahilldesigns.com/

Thanks!

A: 

Try like this:

if (window.location.hash != null && window.location.hash.length > 0) {
    $('a#viewapart').trigger('click');
}
Darin Dimitrov
how frustrating - this seems to work in that the tab is clicked (it is the highlighted one), but the tabbed content isnt showing. Only if I click another tab then back to it again it shows. At least I am one step closer!
Chris
A: 

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');
    }
});
BGerrissen
this has the same effect as above - the tab appears to have been clicked, but the content of the tab is just not showing. The url is here: http://bit.ly/cg7cw0 - on the 'apartments' tab it brings up a list of apartments, clicking the 'next page' button refreshes the page with the next lot of apartments, and it is here that I want to automatically switch to the apartments tab again, using the hasg in the url as a trigger. Is there an easier way of doing this?
Chris
Change it to $(document).ready(), not $(window).load()
BGerrissen
A: 

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>
Mic
+1  A: 

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');
    }
});
BalusC
This works! But now it seems so slow to load all the pictures before sliding to the next slide. I'm sure I'm missing a much easier way somewhere - but thanks!
Chris