How do I check whether a certain div
exist on my page and if not, redirect the visitor to another page?
You will need to use JavaScript to be able to check if the element exists and do the redirect.
Assuming the div has an id (e.g. div id="elementId") you can simply do:
if (!document.getElementById("elementId")) {
window.location.href = "redirectpage.html";
}
If you are using jQuery, the following would be the solution:
if ($("#elementId").length === 0){
window.location.href = "redirectpage.html";
}
Addition:
If you need to check the content of divs for a specific word (as I think that is what you are now asking) you can do this (jQuery):
$("div").each(function() {
if ($(this).text().indexOf("copyright") >= 0)) {
window.location.href = "redirectpage.html";
}
});
Using jQuery you can check it like this:
if ($("#divToCheck")){
// div exists
} else {
// OOPS div missing
}
or
if ($("#divToCheck").length > 0){
// div exists
} else {
// OOPS div missing
}
or
if ($("#divToCheck")[0]) {
// div exists
} else {
// OOPS div missing
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With