0

I need to reload the page after I update my database via ajax. In my ajax post method, I have something like the following:

success: function (data) { ....(some code doesn't matter) document.location.reload(); ....(some other code I wish to execute after document is reloaded) } 

However, the code after reload() will be executed first and then page will reload. What am I doing wrong?

1
  • When you "reload" the page, it does just that; it reloads the page as if it was the first time you came to the page. Can you not reset the page by returning whatever data you need to reset the page from your ajax call? You can then take that data and make any updates that are necessary using jquery. Commented Feb 25, 2013 at 20:03

2 Answers 2

1

When page is reloaded the code after document.location.reload(); is executed on the old instance of page, not the reloaded one.

You will need to add a parameter to the current location and do onDocumentReady on a breach that tests the parameter on the document.location to add the code you want. ex:

document.location = document.location.href + "?afterReload=true"; $(document).ready(function() { if ( window.location.search.substring(1) != '' ){ //do something } }); 
Sign up to request clarification or add additional context in comments.

3 Comments

change "window.location.search.substring(1)" to actually look for the query string you just made , not just any one
Ended, it was an example, you should not add "?afterReload=true" if you have parameter already. But for an example I think the idea was needed.
Thanks, dude. I am trying to implement this.
1

Instead of doing a reload, redirect the page and add a variable at the end

window.location.replace("http://example.com/yourpage.php?extravariable"); 

You can then add some code to check for the GET and run the javascript code if the get value is set.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.