tags:

views:

248

answers:

2

Hi

I think this may not be possible, will try and explain as best as I can. I have a page containing tabs (jquery powered), controlled by the following:

I'm using this code, as provided by another user from a previous question.

<script type="text/javascript">
    $(function() {

     $('html, body').animate({scrollTop:0}); // this is my "fix"

        var tabContent = $(".tab_content");
        var tabs = $("#menu li");
        var hash = window.location.hash;
     tabContent.not(hash).hide();
        if(hash=="") {
      $('#tab1').fadeIn();
     }
        tabs.find('[href=' + hash + ']').parent().addClass('active');

        tabs.click(function() {
            $(this).addClass('active').siblings().removeClass('active');
            tabContent.hide();
            var activeTab = $(this).find("a").attr("href");

            $(activeTab).fadeIn();
           return false;
        });

    });
</script>

this code works great when I visit the "tabs" page directly.

however, I need to link to invidual tabs from other pages - so to do this, the code gets the window.location.hash then shows the appropiate tab.

the page doesn't "jump" to the anchor because of "return false".

this event is only triggered on a click event however. hence, if i visit my "tabs" from any other page, the "jump" effect is triggered. To combat this I automatically scroll to teh top of the page, but I would rather this didn't happen.

is there any way to simulate "return false" when the page loads, preventing the anchor "jump" from occuring.

hope this is clear enough.

thanks

+3  A: 

Does your fix not work? I'm not sure if I understand the question correctly - do you have a demo page? You could try:

setTimeout(function() {
  if (location.hash) {
    window.scrollTo(0, 0);
  }
}, 1);

Edit: tested and works in Firefox, IE & Chrome on Windows.

dave1010
hero! Yes, my "fix" did work, but the page would "scroll" (as my code told it to), and I would rather it didn't scroll.your code works perfectly. I hadn't looked at ScrollTo - is the function even necessary? it appears to work fine with just window.scrollTo(0, 0);
Ross
I tried it in Firefox on an empty page without the `setTimeout` and it didn't always work. You probably don't need it if it's in jQuery's `$(function() {` anyway. You don't really need the `location.hash`, but it's nice leaving it in so browsers may not have to do anything. Also reminds you what the scrollTo is for!
dave1010
just tried it in FF/IE/Chrome and as you say, without the "function" it wasnt 100% consistent. Leaving it in and it works great in all 3. much obliged
Ross
+2  A: 

There are other ways of tracking what tab you're on; perhaps setting a cookie, or a value in a hidden field, etc etc.

I would say that if you don't want the page jumping on load, you would be better off using one of these other options rather than the hash, because the main reason for using the hash in preference to them is to allow exactly what you're wanting to block.

Another point - the page won't jump if your hash links don't match the names of the tags in the document, so perhaps if you want to keep using the hash you could manipulate the content so that the tags are named differently. If you use a consistent prefix, you will still be able to use Javascript to jump between then.

Hope that helps.

Spudley
+1 for the alternative ideas, hadn't thought of that approach. thanks
Ross
Good points. Don't forget to try to keep the page usable / accessible with JS / CSS turned off.
dave1010