Why does the following JavaScript script open a new window, but fails to scroll down the page? (Note that I ran this script in the Web Console in Firefox 4.)
w=window.open("http://stackoverflow.com");w.scrollTo(0,150);
How can I open a page in a new browser window and instruct that window to scroll to a specific position?
If you own both domains, you can use window.postMessage to communicate the scroll position to the other window.
In one page you make the postMessage, and in the other you add an event listener.
If you need to support older browsers, you can use window.name to transfer some data between windows.
If you don't own both domains, you're out of luck, due to the SOP. It is a built-in protection in browsers to avoid cross domain abuses.
Derek is correct about the cross-domain security preventing you from doing this. So one answer is to disable the domain security by loading chrome: Chrome --disable-web-security This from THIS chrome instance, run your javascript. The child windows will inherit the load switch and your scrollTo will now work.
You're trying to scroll before the window is ready. Notice that the following works:
It would be best to execute the scroll in a
w.onload
or DOM ready function, but I can't seem to get that to work.The script does not work because it breaches cross-domain security. See this and this. Chrome reports a similar error:
I found something interesting on this...
I've always known you can scroll to an anchor with a name -- in fact, that's the way we were all taught. But I just tried to scroll to a div with an id and it worked!
So, for example, if the target page has a div with id="bobo" then the link http://www.example.com/index.php/home#bobo just worked for me.
Perhaps it's flaky behavior on my end. I feel like I would have heard of this before if it were possible. But all I know is I was trying to do the same thing and for whatever reason it's working.
FWIW, the link I'm using is http://www.religionnews.com/index.php?/rnsblog#blog