0

This is a part of my js:

window.location.reload();
$("#status").html('Try');

I expected the page to reload and then write "try" into the div "#status". But the reverse happens: The div "#status" is written into for a short period, then it disappears and then the page is reloaded where "#status" is empty again.

How do I get them to execute in the correct order?

11
  • 3
    It looks like it will always reload because each time the page loads it will reload again cozing the second code never to be executed. You can see the efect of status being changed but eventualy the page will keep reloading. Commented Aug 13, 2012 at 3:33
  • 2
    And, you're missing a semicolon Commented Aug 13, 2012 at 3:33
  • 1
    reload() just gets js to tell browser to resend a GET request to the current page, it doesn't wait until the reload finishes and continues to the next line of the script. Commented Aug 13, 2012 at 3:35
  • 1
    No he isn't missing a semicolon. They are only mandatory in inline event handlers Commented Aug 13, 2012 at 3:36
  • 2
    @Alegro write the content on page load instead..? like $(window).load(function() { $('#status').html('try'); }); Commented Aug 13, 2012 at 3:38

3 Answers 3

2

window.location.reload(); sends the event to the browser. So technically the event has been executed and it moves onto the next line.

What your comment above seems to imply that you want is more of a load once, and then add something to a div. I would maybe use the hashes in the URL to read this...

var pageHash = window.location.hash;
if(pageHash.indexOf("firstLoad") == -1) {
    //The page hasn't been loaded, so reload
    window.location = window.location.origin + window.location.pathname + "#firstLoad";
} else {
    $("#status").html('Try');
}
Sign up to request clarification or add additional context in comments.

1 Comment

thankyou, jamie, it's not a real solution (too long to explain), but it's very usefull.
2

If this is in a method you execute, you may see the effect of the change of html because the page did do start in time to reload. But this code is being executed with the right order. Its just that location.reload take more time to be executed then simply changing the innerHtml of an element.

9 Comments

what a stupid behaviour. I want to execute the code line by line, and not create a horse racing. It's so normally that next line cannot be executed before previous one is finished.
@Alegro - You have the wrong idea about how window.location.reload(); works. It reloads the whole page including the JS, so the idea of continuing the current function after the reload simply doesn't apply.
well its async my friend. when reload starts the rest of the code continues but the reload will occure when the browser is ready and will stop any execution
If you need your code to stop you can do this window.location.reload(); return; $("#status").html('Try'); But its usless since the rest of the code will never be executed anyway.
as a rule of thumb, window.location.reload(); is always at the end of any activity..
|
0

Alegro this could help in your case

setTimeout(function(){window.location.reload();},5000); 
$("#status").html('Try');

So the message will get displayd and also after 5 seconds the page will get reloaded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.