Redirect using url parameters

2

This one is a redirection code, I used it without any url parameters, but now I need it to work according to the parameters and I could not, I'm pretty new to javascript, could anyone explain what I'm doing wrong in my code?
If ver is equal to 1 , do nothing, otherwise run window.location.replace("https://www.google.com");

var url = new URL(window.location);
var ver = url.searchParams.get("ver");

document.onload = function() {

   if(ver == 1) {
      return false;
   }

   else {
      window.location.replace("https://www.google.com");
   }
}
    
asked by anonymous 21.07.2018 / 03:31

1 answer

1

The onload event is not applicable to document , but to window object.

Change to:

window.onload = function() {...

Now, would not it be more interesting to check if ver is different from 1 , since else will consider any value other than 1 ?

if(ver != 1) {
  window.location.replace("https://www.google.com");
}
    
21.07.2018 / 04:08