简体   繁体   English

如何自动滚动到文本文件窗口的底部?

[英]How to automatically scrolling to the bottom of a text file window?

I have a button that opens a TXT file in a new window. 我有一个在新窗口中打开TXT文件的按钮。 Is there a way to automatically go to the very bottom of that page using javascript or php? 有没有一种方法可以使用javascript或php自动转到该页面的最底部? Or to any particular location (like searching for a string)? 或到任何特定位置(例如搜索字符串)? Because it is a TXT file, there are no anchors. 因为它是TXT文件,所以没有锚。

Here's my button's onclick: 这是我按钮的onclick:

onclick="window.open('comments.txt','_comments').focus();" 

I have looked into adding this to the onclick (but it did not work): 我已经研究过将其添加到onclick中(但没有用):

w.scrollTo(0,150);

It's actually very easy to do: 实际上很容易做到:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>This is a test</title>
</head>
<body>

    <button id="open">Open text file</button>

    <script>
        document.getElementById('open').onclick = function(){
            window.open('comments.txt','_comments').onload = function(){
                this.scrollTo(0, 99999); // Use the biggest value you can
            };
        };
    </script>

</body>
</html>

Make sure you do this from a server (not locally), as browsers check that the files are on the same domain (for security reasons). 确保从服务器(而不是本地)执行此操作,因为浏览器会检查文件是否在同一域中(出于安全原因)。 If you want to work directly on your machine, install a local server and use http://localhost/ . 如果要直接在计算机上工作,请安装本地服务器并使用http://localhost/

Note: Here, I scroll to 99999px, because without an actual HTML document, we're not able to find out the document height. 注意:在这里,我滚动到99999px,因为如果没有实际的HTML文档,我们将无法找到文档的高度。 If that's not enough, use a higher value. 如果这还不够,请使用更高的值。

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

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