location.hash in an iframe scrolls the parent window
I have a page with an iframe. Inside the iframe is code (that I can't change) that sets location.hash to the id of an element in the iframe window.
This has the unwanted effect of scrolling my o开发者_如何学Goutermost browser window so that the top of the window touches the top of the iframe. This is quite annoying as I have a toolbar above the iframe that is vital to my app.
Is there any way of preventing the setting of location.hash affecting the scroll position of the main window?
Will preventDefault help me out here?
EDIT: It looks like this isn't really possible in the way I described it.
If preventDefault doesn't do what you want it to, you could do is dynamically move the location of the <a name='iframehash'>
so that it's always at the top of the screen. That way, when the call to move to whatever hash it's going to gets called, nothing will actually move.
But if preventDefault() works, it's a much better solution.
You can use:
event.preventDefault()
This will stop the default action of the element it is applied to.
https://developer.mozilla.org/en/DOM/event.preventDefault
精彩评论