-8

I'm looking for a small javascript code which will check for a div class and if it's not there then it will redirect the page to a site which is specified in javascript code.

I have this code, but I want it in document.getElementById form because I'm learning and I tried making the code. The below code is jQuery which I found here on this site.

function check() {
  if ($('#demodivclass').length === 0) {
    redirect(somesite.com);
  }

Can anyone help?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Sumant Singh
  • 11
  • 1
  • 3

6 Answers6

2

To check if a class exists on an element you can use classList.contains(). Then you can redirect with location.assign(), like this:

function check() {
  if (!document.getElementById('demodivclass').classList.contains('foo')) {
    window.location.assign('http://somesite.com');
  }
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

Try this code in order to check either a div has a specific classname and redirect after condition meets by using JQuery.

JQuery

if($( "#yourdivId" ).hasClass( "classname" ))
{
  window.location.replace("https://stackoverflow.com");  
}

Javascript

if(document.getElementById('yourdivId').className == 'classname') 
{
   window.location.replace("https://stackoverflow.com");  
}

Hope it helps!

Adeel Siddiqui
  • 210
  • 1
  • 4
0

What the jQuery part of your code is doing is looking for elements with the id demodivclass using the ID selector. Using the document.getElementById() function as shown below will achieve the same thing.

If you want to check if a div that has a specific class exists (based on your question) the document.querySelector() function can be used instead.

To redirect the page use either location.replace or location.assign. Differences are highlighted at Difference between window.location.assign() and window.location.replace()

Redirect if ID does not exist:

function check() {
  if (!document.getElementById('demodivclass')) {

    window.location.replace("somesite.com");
    // or
    window.location.assign("somesite.com");
  }
}

Redirect if class does not exist:

function check() {
  if (!document.querySelector(".demodivclass")) {

    window.location.replace("somesite.com");
    // or
    window.location.assign("somesite.com");
  }
}
H77
  • 5,859
  • 2
  • 26
  • 39
  • I believe the code is checking for a div class and if present then redirecting to another site. But, I want just opposite of this case. The code should first check for a div id. Then if it's there no redirection should happen. If divid is not there then only redirection should happen. – Sumant Singh Jul 28 '17 at 16:10
  • @SumantSingh that is what the first code block in my answer does. – H77 Jul 29 '17 at 02:34
0

First of all to select your DOM element, you need to use getElementById with your ID that you defined previously. Then, to check if there is a class settled, you need to use classList.contains() method. Finally, to make a URL redirection, use window.location.href = 'Your link'.

function check() {
    if (document.getElementById('element').classList.contains('class')) {
        window.location.href = 'newPage.html';
    }
}

Good luck and keep learning.

Arrabidas92
  • 1,113
  • 1
  • 9
  • 20
  • Not sure why this got a downvote while an identical answer got an upvote! Maybe add a bit more info as this is otherwise essentially just a 'code-only' answer. – freedomn-m Jul 27 '17 at 08:52
  • Yes, do you not have an 'edit' button at the bottom of the answer? – freedomn-m Jul 27 '17 at 08:59
  • Oh yeah ! I see it. – Arrabidas92 Jul 27 '17 at 09:01
  • As I'm still learning I might make mistake understanding your code. But, I think it's not fulfilling my need. What I need is a code that will look for a div class by getelementbyid and then starts the condition. If divclass is there then code should not redirect but if it's not there then there should be a redirect. – Sumant Singh Jul 28 '17 at 16:14
0

if($( "#div" ).hasClass( "divClass" )) /// check Is Exists or not
      location.assign("https://stackoverflow.com");
 else
      alert('class not exist!')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div id="div" class="divClass"></div>

try this one. I hope it helps you.

Neeraj Pathak
  • 759
  • 4
  • 13
-1
function check() {
  if ($('#demodivclass').length < 1) {
    window.location.href = 'somesite.com';
  }

Please try the following code, it is similar to your code. I just changed a little code of checking statement.

Ilove112
  • 94
  • 1
  • 10
  • I'm not looking for jquery code. I think it's jquery in your answer. Is it possible to make it in getelementbyid format? – Sumant Singh Jul 28 '17 at 16:17
  • `if (!document.getElementById("somediv")) ` You can try this in your if statement. It check if somediv doesn't exist then you can do something. – Ilove112 Jul 31 '17 at 01:59