最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Run JS after page reload - Stack Overflow

programmeradmin7浏览0评论

Can I run javascript Code after reloading the page?

In my case I want to enter the code in the address bar via javascript: <<CODE>>

to reload a page several times and check if a button with a special id exists and to click it. Here is my Code:

function check () {
    if(new Date().getTime >= 1442949420000) {
        var loginbtn = document.getElementById('loginbutton');

        if(loginbtn){
            loginbtn.click();
        } else {
            console.log('button not yet here - reload');
            window.location.reload();
            check();
        };
    } else {
        console.log('to early - reload');
        window.location.reload();
        check();
    };
};
check();

The problem is, that the page reloads after window.location.reload() and the check() function never gets called, so the page just reloads once.

Or is there a service, that injects my script to a url?

Can I run javascript Code after reloading the page?

In my case I want to enter the code in the address bar via javascript: <<CODE>>

to reload a page several times and check if a button with a special id exists and to click it. Here is my Code:

function check () {
    if(new Date().getTime >= 1442949420000) {
        var loginbtn = document.getElementById('loginbutton');

        if(loginbtn){
            loginbtn.click();
        } else {
            console.log('button not yet here - reload');
            window.location.reload();
            check();
        };
    } else {
        console.log('to early - reload');
        window.location.reload();
        check();
    };
};
check();

The problem is, that the page reloads after window.location.reload() and the check() function never gets called, so the page just reloads once.

Or is there a service, that injects my script to a url?

Share Improve this question asked Sep 22, 2015 at 20:32 Daniel Alexander BeneschDaniel Alexander Benesch 2812 gold badges3 silver badges8 bronze badges 3
  • 1 your function will be called anyway after reload because you call it below declaration. no need to call it below reload – guramidev Commented Sep 22, 2015 at 20:36
  • If you're calling it from the address bar (as in manually) I don't know of a way to make it work in a browser since JS scripts stop executing on page reload. You could store state (e.g. cookie, localstorage, etc.) and rerun the script (enter again, save in a bookmarklet) to pick up from where it left off. An alternative would be to use a browser extension (e.g. userscripts, tampermonkey, etc.) or use a non-browser to navigate/manipulate the page (e.g. phantomjs, casper, etc.) – arcyqwerty Commented Sep 22, 2015 at 20:38
  • I don't think that will work like you think it does, Gurami. The page will RELOAD and will not be at that same point in the JavaScript method. – Matt Runion Commented Sep 22, 2015 at 20:38
Add a ment  | 

3 Answers 3

Reset to default 11

You can add a URL fragment to the address bar, and then reload the page. So, before the reload, your would be at http://yoursite./yourPage and after the reload, you could be at http://yoursite./yourPage#reload. Here is a simple working example for something along those lines:

    document.getElementById("clickMe").onclick = function clicked(){
    	window.location.hash = 'reload';
    	window.location.reload();
    }
    
    //When the document has loaded, call the function
    document.addEventListener("DOMContentLoaded", function(event) { 
    	if(window.location.hash == "#reload"){
    		console.log("The Page has been reloaded!");
    	}else{
    		console.log("The page has a new hit!");
    	}
    });
    <button id="clickMe" type='button'>click</button>

When you click the button, the hash is added to the window location, and then the page is reloaded, with the URL fragment attached. I then decide in the document ready function if the page has been reloaded or not. Make sure to test it locally.

Another approach if you want to run your code from browser console can be:

  1. Open developer tools on the page that you want to test PageA
  2. Then open the same page in a popup window using javascript executed in browser console on PageA (it must be the same page to avoid cross origin errors)
  3. Save the popup reference in a javascript variable
  4. Now you can do whatever you want with the page in the popup window even if it reloads

EXAMPLE:

function check() {
  var newwindow = window.open('https://yoursite.', 'WindowTitle', 'height=200,width=150');
  if (new Date().getTime >= 1442949420000) {
    var loginbtn = newwindow.document.getElementById('loginbutton');

    if (loginbtn) {
      loginbtn.click();
    } else {
      console.log('button not yet here - reload');
      newwindow.location.reload();
      check();
    };
  } else {
    console.log('to early - reload');
    newwindow.location.reload();
    check();
  };
};
check();

Personally I would not modify the URL. That can be ok if you send the user to another URL after your button click but otherwise you may be stuck with that modified URL and the function will execute each time the user manually refreshes the page if he decides to do so.

I would instead set a sessionStorage item before reload, read it on reload to execute the function if ready, or reload again if not, and then get rid of it when I am done.

Now I don't see how reloading a page multiple times until the condition is met is a good practice. Why not use a setInterval to check the condition instead?

发布评论

评论列表(0)

  1. 暂无评论