views:

1170

answers:

5

I need to prevent the automatic scroll-to behavior in the browser when using link.html#idX and <div id="idX"/>.

The problem I am trying to solve is where I'm trying to do a custom scroll-to functionality on page load by detecting the anchor in the url, but so far have not been able to prevent the automatic scrolling functionality (specifically in Firefox).

Any ideas? I have tried preventDefault() on the $(window).load() handler, which did not seem to work.

Let me reiterate this is for links that are not clicked within the page that scrolls; it is for links that scroll on page load. Think of clicking on a link from another website with an #anchor in the link. What prevents that autoscroll to the id?

Everyone understand I'm not looking for a workaround; I need to know if (and how) it's possible to prevent autoscrolling to #anchors on page load.


NOTE

This isn't really an answer to the question, just a simple race-condition-style kluge.

Use jQuery's scrollTo plugin to scroll back to the top of the page, then reanimate the scroll using something custom. If the browser/computer is quick enough, there's no "flash" on the page.

I feel dirty just suggesting this...

$(document).ready(function(){

    // fix the url#id scrollto "effect" (that can't be
    // aborted apparently in FF), by scrolling back
    // to the top of the page.
    $.scrollTo('body',0);

    otherAnimateStuffHappensNow();

});

Credit goes to wombleton for pointing it out. Thanks!

A: 

Sure, you need to make sure to return false.

<a href="#name" onclick="return false">Click me</a>

You can also run any javascript function and just make sure that you return false at the end

<a href="#name" onclick"myFunc()"> Click me </a>

and:

function myFunc() {
 //do something
return false;
}
Naoric
It needs to handle links in the address bar, not clicked on the page. You're assuming I want to click a link on the page and prevent it from scrolling; in fact, I want to take a link and essentially paste it into the address bar and navigate to it. How do I prevent that autoscroll effect?
Jared Farrish
+1  A: 
  1. Scroll first to top (fast, no effects pls), and then call ur scroll function.(I know its not so pretty)
  2. or just use a prefix
Stupid2.de
I've thought of that, but my markup/css designer would probably castrate me for suggesting that.
Jared Farrish
i mean add the pre/suffix to the anchor in the url...
Stupid2.de
That's a no go (already thought of that). They need to make the links straightforward. Since it's a workaround, and not a solution to the actual problem, I'll keep it in mind.
Jared Farrish
1. is about the only unobtrusive method I found, but it's not the answer to the problem (at least in a pure sense).
Jared Farrish
+3  A: 

This seems the only option I can see with ids:

$(document).ready(function() {
  $.scrollTo('0px');
});

It doesn't automatically scroll to classes.

So if you identify your divs with unique classes you will lose a bit of speed with looking up elements but gain the behaviour you're after.

(Thanks, by the way, for pointing out the scroll-to-id feature! Never knew it existed.)

EDIT:

wombleton
This doesn't answer the question - how to prevent the autoscroll on page load to #anchor? Thanks though - it's just my html/css coder won't go for it.
Jared Farrish
Strictly speaking it's #id. An anchor's another thing to what you're doing. No idea. Resetting ondomready is probably as good as you're going to get.
wombleton
I say #anchor so people know what I'm talking about. I'm not sure that OnDOMReady is not the same as $.ready(), in which case this acts after the scrollto effect on load. I'm starting to think it's not actually possible to prevent the page load to (ahem) #id.
Jared Farrish
By the way, here's an example: http://balcomagency.com/en/balcom.html#buzz
Jared Farrish
Check this: http://www.inmotiondesign.nl/playground/?p=20
wombleton
My tests with $(window).ready() seem to demonstrate in the balcom case the page scrolls before ready(). Try it by downloading the balcom page and using base href.
Jared Farrish
I didn't read this correctly initially. My apologies. It's still a kluge (see my question note), but so far the only reasonable way to do it unobtrusively. Thanks!
Jared Farrish
If you can edit entry, I can revote it back up. I'm assuming something slight would work.
Jared Farrish
A: 

The example page (http://www.landventuresllc.com/en/landventures.html#services) does it by scrolling to

window.location.href.indexOf('#');

toward the end of the $(document).ready() function.

Try (untested)

$(window).scrollTo(window.location.href.indexOf('#'))
willoller
That's hilarious. I actually wrote that code. Here's a page using virtually the same code that doesn't work if it's not the x,y coordinate (which I assume prevents the autoscroll, but I don't actually know why it works on LV): http://balcomagency.com/en/balcom.html#buzz
Jared Farrish
A: 

This worked well for me:

1- put this on your css file

a[name] { position: absolute; top: 0px }

2- put this on your document.ready bind right before you start animating (if you're animating at all)

$("a[name]").css("position","relative");

Might need tweaking depending on your stylesheet/code but you get the idea.

Credit to: http://cssbeauty.com/skillshare/discussion/1882/disable-anchor-jump/

Christian M. M.