重定向至If-Javascript诊断树

Redirect If - Javascript

本文关键字:诊断 If-Javascript 重定向      更新时间:2023-09-26

当"this._try==1"时重定向不起作用?

这是完整的JS,但它不再在点击时检查try==1,而是在窗口关闭时自动检查。

function ouvre(fichier) {
  ff=window.open(fichier,"popup","width=600px,height=300px,left=50%,top=50%")
  //this._try = 1;     
  setTimeout('this._try = 1;', 4000);
}

function playMovie(_try) {
  if (this._try == 1) { playsavideo(); }
  else { alert('You must share to unlock.'); }
}
function playsavideo(type) {
  {
    window.location = "http://google.com"

  }
}

这个窗口被调用。。。

<a href="#" onClick="ouvre('https://twitter.com/share?url=https%3A%2F%2Fdev.twitter.com%2Fpages%2Ftweet-button');return false">Test</a>

您尝试使用this作为值/全局变量的载体
this是一个"相对"变量,它总是与它所在对象的实例相关。
在您的代码中没有实例。即使是,函数内的this也会引用一件事,而函数之外的任何其他this都很可能引用另一件事。

而全球vars并不是一种好的做法。试试这个:

var i_am_a_global_var = false;
function ouvre(fichier) {
    ff=window.open(fichier,"popup","width=600px,height=300px,left=50%,top=50%");
    setTimeout(function(){window.i_am_a_global_var=true;bobo();}, 4000);
}
function bobo(){
  if (window.i_am_a_global_var) {
    window.location.href = "http://www.google.com/" 
  }
}
  1. 定义全局变量
  2. 当超时发生时,它将回调一个闭包(function(){...}),该闭包将调用bobo函数
  3. 如果全局var为true,那么bobo函数就是重定向发生的地方

你这样做可能是因为其他逻辑可能会改变你的this._try,但如果你只想在x秒后重定向,那么短版本是:

window.open(fichier,"popup","width=600px,height=300px,left=50%,top=50%");
setTimeout('window.location.href = "http://www.google.com/"', 4000);

您可以添加一个内容为window.location.href = "http://www.google.com/"的新函数,然后编辑setTimeout('[function name]',4000)。

您必须将函数传递给setTimeout。我认为你应该在函数中进行重定向。

var that = this;
function ouvre(fichier) {
    ff=window.open(fichier,"popup","width=600px,height=300px,left=50%,top=50%");
    //this._try = 1;
    var fn = function() {
       that._try = 1;
       window.location = 'http://google.com';
    }
    setTimeout(fn, 4000);
}