简体   繁体   English

此JavaScript代码有什么问题?

[英]What's wrong with this javascript code?

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? 谁能告诉我为什么页面在所有情况下都可以刷新?

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

if (newprod) { // not null, undefined or 0
  $.ajax({
    //posting code here
  });
}

if (act) { window.location.reload(1); }

assuming that is what the code was supposed to do. 假设这就是代码应该做的。 document.location is deprecated and in theory read-only. document.location已弃用,并且在理论上是只读的。

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 ''. 之所以可以一直运行,是因为newprod与''不同。

As the question is what is wrong with that JavaScript code i will advise. 因为问题是我将建议该JavaScript代码出了什么问题。

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

You may want to learn more about false-y values in JavaScript . 您可能想了解有关JavaScript中false-y值的更多信息 My guess is that your if statement is giving you some problems because it does not compare the way you think it should compare. 我的猜测是,您的if语句给您带来了一些问题,因为它无法按照您认为的比较方式进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM