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

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

我有一个按钮可以在新 window 中打开 TXT 文件。有没有办法使用 javascript 或 php 自动转到该页面的最底部?或者到任何特定位置(如搜索字符串)?因为是TXT文件,所以没有锚点。

这是我的按钮的点击:

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

我考虑过将其添加到 onclick(但没有成功):

w.scrollTo(0,150);

其实很容易做到:

<!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>

确保从服务器(而不是本地)执行此操作,因为浏览器会检查文件是否在同一域中(出于安全原因)。如果您想直接在您的机器上工作,请安装 local server 并使用 http://localhost/.

注意: 在这里,我滚动到 99999px,因为没有实际的 HTML 文档,我们无法找出文档高度。如果这还不够,请使用更高的值。