3

I have the following code to reload page only once after submitting some data using JQuery.

code to reload page :

update: the url here is not ending with '?' because it has parameter value for example: http://localhost:49208/UserView.aspx?id=12

  var url = window.location.href;
    if (url.indexOf('?') > -1) 
        {
         window.location.href = url;
        }

The problem here is that page reloading does not stop?

1
  • 3
    add a new query string key value that says &alreadyReloaded=yes and check for it in your javascript Commented Nov 27, 2015 at 2:59

5 Answers 5

2

The reason it won't stop reloading is because you aren't changing the conditions of the url; so if the if statement is ever true, it will happen again and again.

If you want to reload the page, just use window.location.reload();

Sign up to request clarification or add additional context in comments.

Comments

2

Try this logic.

if (url.indexOf('?') == -1) {
    url = url + '?';
    location = '?';
    location.reload(true);
}

Comments

2

Easiest way is to add a flag variable so that javascript can check whether the page is reloaded previously.

 var url = window.location.href; // get the current url of page into variable
 if (url.indexOf('?') > -1) { // url has a '?'
     if(url.indexOf('reloaded') < 0){ // url does not have the text 'reloaded'
          url = url + "&reloaded=true"; // add the word 'reloaded' to url
          window.location = url; // "reload" the page
     }
 }

Comments

2

If you want to reload the page only once, use the following method:

    if(!window.location.hash) {
       window.location = window.location + '#loaded';
       window.location.reload();
     }

Comments

1
if(!window.location.hash) 
{
    window.location = window.location + '#loaded';

    window.location.reload();
}

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.