-2

I'm trying to use this script to iframe a page into itself, and refresh the iframe at an interval:

document.write('<iframe id="frame" src="' + window.location.href + '"><script>setInterval(function ()/{document.getElementById("frame").contentWindow.location.reload();},10000);</script>');

All it does is append '); to the end of the page.

I am using this as a script on someone else's page to auto refresh. If I just refreshed, the interval would have been destroyed.

not my real name
  • 393
  • 4
  • 16

2 Answers2

1

So While I'm still unclear of what you're trying to get to with this and your question was rather unclear (in retrospect), I was curious about how can we solve this without creating an infinite loop.

window.top !== window.self

won't work because they are one and the same, so I figured that will use a hash on the original page, omit it from the child to differentiate one from its own clone.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>test</title>
</head>
<body>

    <h1 id="h1"></h1>
    <script type="text/javascript">
        document.getElementById('h1').textContent =  window.location.search;
    </script>


<script type="text/javascript">

    if (window.location.hash === '#origin') {

        var ifr = document.createElement('iframe');
        var props = {
            src: window.document.location.href + '?r=0',
            width: 500,
            height: 300,
            style: "border:1px solid red;width:500px;height:300px;"
        }

        for (var key in props) {
            ifr.setAttribute(key, props[key]);
        }

        document.body.appendChild(ifr);

        setInterval(function(){
            var s = ifr.src.split('?r=')[0];
            ifr.setAttribute('src', s + '?r=' + parseInt( Math.random() * 1000000))
            console.log(ifr.src);
            // you can skip this, but it illustrates the refreshes
            document.getElementById('h1').textContent = ifr.src;
        }, 2000);
    }

</script>
</body>
</html>

You will have to run this on your own, the code snippet dislikes its attempt for redirect and I cannot disagree..., - NB: only ran it in Chrome, but there's much that should be different elsewhere

Also, just for the sake of clarity, if you save this as test.html then you should run it as path/to/your/file/test.html?r=378426#origin

datafunk
  • 636
  • 9
  • 18
0

Took a bit of a shortcut with how the query string is handled, but the gist of it is here.

Create an iframe object, add its properties then add to the DOM and finally reload the page with a new query string every so often

    var ifr = document.createElement('iframe');
    var props = {
        src: 'http://example.com/?r=0',
        width: 500,
        height: 300,
        style: "border:1px solid red;width:500px;height:300px;"
    }

    for (var key in props) {
        ifr.setAttribute(key, props[key]);
    }

    document.body.appendChild(ifr);

    setInterval(function(){
        var s = ifr.src.split('?r=')[0];
        ifr.setAttribute('src', s + '?r=' + parseInt( Math.random() * 1000000))
        console.log(ifr.src);
    }, 10000);
datafunk
  • 636
  • 9
  • 18
  • so you want to frame the page into itself? May I ask why? – datafunk Aug 12 '16 at 02:09
  • As I suspected (but quickly ran it to sanity check myself), if you run such a script on a page, into which you embed the same page, then keep refreshing it you end up with an elevator mirror effect. ```src: window.document.location.href + '?r=0'``` should work with my previous answer, but I can't wrap my head around why you'd want to do that... – datafunk Aug 12 '16 at 02:17
  • I originally intended to run this in the console of the Slack Electron app for faster message updates. – not my real name Oct 22 '20 at 15:11