简体   繁体   English

在JS中打开一个新窗口,但浏览器会自动自动向下滚动到底部

[英]Open a new window in JS but the browser is scrolled straight down to the bottom automatically

I was developing a struts web application, in one of my JSP I created a button to open a new window. 我正在开发一个struts Web应用程序,在我的一个JSP中,我创建了一个按钮来打开一个新窗口。 The way I did it is: 我这样做的方式是:

window.open(location,'_blank');

everything works fine except the new window is always scrolled down to the bottom itself. 一切正常,只是新窗口始终向下滚动到底部。 How to show the top part instead? 如何显示顶部呢?

I am using Chrome. 我正在使用Chrome。

Include place this JS at the bottom of the page just before the closing body tag. 在关闭的body标记之前,将此JS放在页面底部。

<script>window.scrollTo(0,0);</script>

Or you could simply use jQuery if your page is already including it: 或者,如果您的页面已包含jQuery,则可以简单地使用jQuery:

<script>$(document).ready(function(){$(window).scrollTop(0);});</script>

Try something like this: 尝试这样的事情:

var newWindow = window.open(someUrl, '_blank');
newWindow.onload = function(){
    newWindow.scrollTo(0, 0);
}
newWindow.onload();

尝试这个

window.open(location,'_blank','top=50');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM