How can I redirect on google site?

90 views Asked by At

How can I redirect those who enter the site with a mobile phone to another page or send am message them on google site?

I used of this code but nothing special happens.

if (window.innerWidth <= 500) {
  window.location.href = "https://example.com/";
}

I even used the alert command, but it didn't show anything. Is it possible to use JavaScript codes in Google site?

1

There are 1 answers

3
Mehdi On

If you want those entering your website via mobile to be redirected to another URL (ex here https://www.example.com/) you can use the window.onload event listener, in that case if website is loaded on small screen (here <700px) they will be immediately redirected to google page. If you want on a resize event you can use the window.resize listener.

let windowWidth
window.onload = function() {       // redirection onload if screen width is <700px
  windowWidth = window.innerWidth;
  if(windowWidth < 700){
    window.location.href='https://www.example.com/'
  }
};

window.onresize = function() {      // redirection if you resize and screen is <700px
  windowWidth = window.innerWidth;
  if(windowWidth < 700){
    window.location.href='https://www.example.com/'
  }
};