mardi 27 février 2018

Using JS to do a redirect in a Content Management System if criteria is met

I am working in a CMS and I am using an html widget to do some redirect code, it is working fine, but of course there is a wrinkle. Basically, I want to create an if statement, because there is a possible message on the initial page. So what I want it to do is if it doesn't see the div - redirect, but if it does see the div, do nothing.

var msgDiv = document.getElementById('thisDiv');

if (msgDiv === null) {
   window.location = "otherpage.html";
} else {
  //do nothing
}

What is happening with this code, is it goes right into redirect regardless. If I do this version:

window.onload = function () {
    var msgDiv= document.getElementById('testDiv');

    if (msgDiv === null) {
       window.location = "otherpage.html";
    } else {
      //do nothing
    }

};

I briefly see the first page and the message, but then it redirects just after a pause. I feel like I am missing one small detail and would greatly appreciate the help.

Aucun commentaire:

Enregistrer un commentaire