1

I have a button that opens a TXT file in a new window. Is there a way to automatically go to the very bottom of that page using javascript or php? Or to any particular location (like searching for a string)? Because it is a TXT file, there are no anchors.

Here's my button's onclick:

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

I have looked into adding this to the onclick (but it did not work):

w.scrollTo(0,150);
user3283304
  • 149
  • 1
  • 2
  • 14
  • It is impossible to execute the javascript in new-opened window. You can only use an iframe. –  Apr 02 '15 at 22:14
  • @Mathematician171 I thought so, but I gave it a try and it actually works! Wonderful. – blex Apr 02 '15 at 22:43
  • @blex Yes, it works on same domain. But, if domains of fisrt and second page are different, it doesn't work. For example, it is not possible to write a javascript code on one site to open a Facebook page and execute a script. –  Apr 03 '15 at 12:52
  • @Mathematician171 That's correct. It would be a nightmare otherwise. – blex Apr 03 '15 at 13:59

1 Answers1

1

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/.

Note: Here, I scroll to 99999px, because without an actual HTML document, we're not able to find out the document height. If that's not enough, use a higher value.

blex
  • 24,941
  • 5
  • 39
  • 72