0

The redirect isn't working when "this._try == 1"?

Here's the full JS, however it no longer is checking the try == 1 on click, rather automatically when the window is closed.

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"


  }
}

The window is called by this...

<a href="#" onClick="ouvre('https://twitter.com/share?url=https%3A%2F%2Fdev.twitter.com%2Fpages%2Ftweet-button');return false">Test</a>
5
  • Try moving it to the callback of setTimeout? Commented Dec 15, 2013 at 2:09
  • That does not look like the code u want to show us. Some pieces are commented out, and why do u use this are you creating an object someware? show all the code? Commented Dec 15, 2013 at 2:10
  • @popnoodles his code still does not make sense, can u understand where what is this this? Commented Dec 15, 2013 at 2:13
  • @popnoodles I added more of the code, sorry! Commented Dec 15, 2013 at 2:18
  • @AndrewRivers someone already answered the question, but it is important that you post chunks of code as you have now, not just cut and paste lines of code. Errors aren't necessarily that straight forward. Commented Dec 15, 2013 at 2:20

3 Answers 3

2

You try to use this as the carrier of the value / global variable.
But this is a "relative" variable, it always relates to an instance of an object it is in.
In your code there is no instance. and even if it was, the this inside the function will refer to one thing and any other this outside the function will most probably refer to another thing.

While global vars are not good practice. Try this:

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. define a global variable
  2. when the time out happens it wil callback a closure (function(){...}) which will call the bobo function.
  3. the bobo function is where redirect happens, if global var is true.

You might did this due to other logic which mught change your this._try but if all you wanted to do is redirect after x seconds, then the short version would be:

window.open(fichier,"popup","width=600px,height=300px,left=50%,top=50%");
setTimeout('window.location.href = "http://www.google.com/"', 4000);
Sign up to request clarification or add additional context in comments.

2 Comments

Someone pointed out it should be window.location.href=... or do both work?
@popnoodles both work, but to be on the safe side, i'll edit to your version
1

you can add a new function with content window.location.href = "http://www.google.com/", then edit setTimeout('[function name]', 4000).

Comments

0

You have to pass function to setTimeout. And I think you should do the redirect in the function.

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);
}

1 Comment

This works as well and it doesn't use global vars so I'm going to implement this. Thank you so much!

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.