在Javascript window.open()中保持滚动位置

时间:2022-10-22 14:58:21

How do I maintain the scroll position of the parent page when I open new window using window.open()? The parent page returns to the top of the page.

当我使用window.open()打开新窗口时,如何保持父页面的滚动位置?父页面返回页面顶部。

Here is my current code:

这是我目前的代码:

<a href="#" onclick="javascript: window.open('myPage.aspx');">
   Open New Window
</a>

4 个解决方案

#1


9  

<a href="#" onclick="window.open('myPage.aspx');return false;">Open New Window</a>
  • javascript: is not required in event attributes.
  • javascript:在事件属性中不是必需的。

  • You were not returning false from the event handler, so the link was being following, it was equivilent to <a href="#">Scroll to top</a>.
  • 您没有从事件处理程序返回false,因此链接正在跟踪,它与滚动到顶部无关。

#2


1  

<a href="javascript:void()" onclick="window.open('myPage.aspx');">Open New Window</a>

Ought to do it. As others have mentioned, the # is trying to go to a non-existent anchor, which will cause the browser to scroll to the top. You don't want to remove the href attribute, because some browsers don't treat <a> tags without href attributes as links for styling purposes, and you would have to define additional CSS rules to ge thte link to look like other links on your site.

应该这样做。正如其他人所提到的那样,#正试图进入一个不存在的锚点,这会导致浏览器滚动到顶部。您不希望删除href属性,因为某些浏览器不会将没有href属性的标记视为用于样式目的的链接,并且您必须定义其他CSS规则以使链接看起来像其他链接你的网站。

Also, why have an href attribute and then try to block the event by always returning false from your handler. In my opinion, this way is cleaner than the others proposed here.

此外,为什么有一个href属性,然后尝试通过始终从您的处理程序返回false来阻止该事件。在我看来,这种方式比这里提出的其他方式更清晰。

#3


0  

I think the problem is that your link is pointing to an empty # (href="#"), which the browser will interpret to mean "the top of the page".

我认为问题是你的链接指向一个空的#(href =“#”),浏览器会将其解释为“页面顶部”。

Try removing the href attribute from your anchor tag. The onclick attribute should be enough for what you need.

尝试从锚标记中删除href属性。 onclick属性应该足以满足您的需求。

#4


0  

It is good to keep the page accessible for those that don't have javascript, or have it disabled:

对于那些没有javascript或禁用javascript的人来说,保持页面可访问是很好的:

<a href="myPage.aspx" target="_blank" onclick="window.open('myPage.aspx');return false;">Open New Window</a>

The target="_blank" is to open in a new window (or tab). If the client does not have JS, then it will still open the page, just not in a JS invoked window.

target =“_ blank”将在新窗口(或选项卡)中打开。如果客户端没有JS,那么它仍然会打开页面,而不是在JS调用的窗口中。

#1


9  

<a href="#" onclick="window.open('myPage.aspx');return false;">Open New Window</a>
  • javascript: is not required in event attributes.
  • javascript:在事件属性中不是必需的。

  • You were not returning false from the event handler, so the link was being following, it was equivilent to <a href="#">Scroll to top</a>.
  • 您没有从事件处理程序返回false,因此链接正在跟踪,它与滚动到顶部无关。

#2


1  

<a href="javascript:void()" onclick="window.open('myPage.aspx');">Open New Window</a>

Ought to do it. As others have mentioned, the # is trying to go to a non-existent anchor, which will cause the browser to scroll to the top. You don't want to remove the href attribute, because some browsers don't treat <a> tags without href attributes as links for styling purposes, and you would have to define additional CSS rules to ge thte link to look like other links on your site.

应该这样做。正如其他人所提到的那样,#正试图进入一个不存在的锚点,这会导致浏览器滚动到顶部。您不希望删除href属性,因为某些浏览器不会将没有href属性的标记视为用于样式目的的链接,并且您必须定义其他CSS规则以使链接看起来像其他链接你的网站。

Also, why have an href attribute and then try to block the event by always returning false from your handler. In my opinion, this way is cleaner than the others proposed here.

此外,为什么有一个href属性,然后尝试通过始终从您的处理程序返回false来阻止该事件。在我看来,这种方式比这里提出的其他方式更清晰。

#3


0  

I think the problem is that your link is pointing to an empty # (href="#"), which the browser will interpret to mean "the top of the page".

我认为问题是你的链接指向一个空的#(href =“#”),浏览器会将其解释为“页面顶部”。

Try removing the href attribute from your anchor tag. The onclick attribute should be enough for what you need.

尝试从锚标记中删除href属性。 onclick属性应该足以满足您的需求。

#4


0  

It is good to keep the page accessible for those that don't have javascript, or have it disabled:

对于那些没有javascript或禁用javascript的人来说,保持页面可访问是很好的:

<a href="myPage.aspx" target="_blank" onclick="window.open('myPage.aspx');return false;">Open New Window</a>

The target="_blank" is to open in a new window (or tab). If the client does not have JS, then it will still open the page, just not in a JS invoked window.

target =“_ blank”将在新窗口(或选项卡)中打开。如果客户端没有JS,那么它仍然会打开页面,而不是在JS调用的窗口中。