vendredi 24 février 2017

Js practise exam help needed

i've been busy learning Javascript and was trying out some practise exams. I came across this script with the question "When the user clicks the link, which site will he navigate to?".

The script is the following:

function switcheroo(where, destination){
    if(where == "http://www.google.com/"){
      this.location = destination;}
    else{
      this.location = "http://www.msn.com";
    }
}

<a href="http://www.google.com/" onclick="switcheroo(this.href, 'http://www.yahoo.com/');return false;">Google</a>

after running this script i know the answer is yahoo. But what i dont get is why. I read it this way.. the function gets 2 parameters from the onclick function, being this.href (google.com) and yahoo.com. Where and destination.

So if where == google (wich is true) then this.location = destination. So the current site would change to yahoo.com, being the input of the function "destination" right?

But that doesnt happen?! If i change the script into "return true" then i get the link to google.com. But when its "return false" i go to yahoo.com. Can anyone explain to a newb how this works?!

Aucun commentaire:

Enregistrer un commentaire