1

I want to display alert message and then refresh the page.

But it take infinite loop of alert messages.

   if(isValid){
      alert("alert some text");
      document.location.reload(true);
   }
   else {
       .............
   }

How can I do that with out infinite loop

OammieR
  • 2,800
  • 5
  • 30
  • 51
  • Find the condition that causes `isValid` to be true and prevent it? – Ates Goral Feb 07 '12 at 06:26
  • By the looks of your code, you're trying to alert the user, and then refresh the page, but the condition still evaluates to true after it's been refreshed. What are you actually trying to achieve here? Are you sure you're not just messing up the condition? – annonymously Feb 07 '12 at 06:27
  • Do you want to run this code until the password and username are correct? – Bry6n Feb 07 '12 at 06:27
  • Are you asking how you can alert only once? – Ates Goral Feb 07 '12 at 06:27
  • the code is correct, I think you're setting the `isValid` variable somewhere before the if block incorrectly. Also I think the if condition should be `!isValid` – fardjad Feb 07 '12 at 06:33
  • @Ates Goral Yes. I want to alert only once. – OammieR Feb 07 '12 at 06:34
  • There are a few ways you do this (alert once), but it would be more productive for all of us if you explain exactly what you're trying to achieve. What is this alert for? And why do you need to reload? Do you need to reload the exact same URL? – Ates Goral Feb 07 '12 at 06:40

1 Answers1

1

You would have to pass along the state. Possibly refreshing the page by pointing itself with a querystring variable. Then read that variable

if (getParameterByName("reloaded") != "yes") {
    alert("username or password are incorrect");       
    window.location = 'mypage.html?reloaded=yes';  
} else {
    // Do something else..
} 

Note the getParameterByName function is found at another stackoverflow answer: How can I get query string values in JavaScript?. You could alternatively use a library such as jQuery for a better solution.

Community
  • 1
  • 1
Prescott
  • 7,312
  • 5
  • 49
  • 70