在JavaScript中,我怎么能在一个新的浏览器窗口中打开一个网页,并滚动到一个特定的位置?

在JavaScript中,我怎么能在一个新的浏览器窗口中打开一个网页,并滚动到一个特定的位置?

问题描述:

为什么下面的的JavaScript脚本中打开一个新窗口,但未能向下滚动页面? (请注意,我在Firefox 4中运行Web控制台此脚本。)

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://*.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?

如果你拥有两个域,你可以使用的 window.postMessage 以滚动位置传达给另一个窗口。
在一个页面中,您做postMessage的,而在其他的你添加事件侦听器。

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.

如果你没有自己的两个领域,你的运气了,由于 SOP 一>。
它是在浏览器中内置的保护,以避免交叉域名滥用。

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.