-4
var act = false;
var newprod = prompt("tell me somthing", "");

if (newprod != '' && newprod != null) {
  $.ajax({
    //posting code here
  });
}

if (act != false) { document.location.href = window.location; }

The page is refresh in every condition even act false or not.

It is fired on an event.

Can anyone tell me why it page is refreshed in all condition?

1
  • 1
    ...and perhaps clarify? What are you expecting -- and what is it actually doing...? Commented Oct 15, 2010 at 5:42

3 Answers 3

0

Assuming that is what the code was supposed to do. document.location is deprecated and in theory read-only.

It does make little sense to ajax and then reload. Better just submit the page and have the server reload or to NOT reload but just update the page with the response data

var act = false;
var newprod = prompt("tell me somthing", "");

if (newprod) { // not null, undefined or 0
  $.ajax({
    success: function(response) {
      //posting code here - I assume will set act to true 
      if (response.act) { window.location.reload(1); }
    } 
  });
}
Sign up to request clarification or add additional context in comments.

Comments

0

This should work

if( newprod != null && newproda.length != 0) {

 //Execute the code

}

To the reason why it was always working was that newprod was not the same as ''.

As the question is what is wrong with that JavaScript code i will advise.

if(act) {
 document.location.href = window.location;
}

Comments

0

You may want to learn more about false-y values in JavaScript. My guess is that your if statement is giving you some problems because it does not compare the way you think it should compare.

Comments