Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
28
rated 0 times [  29] [ 1]  / answers: 1 / hits: 55842  / 13 Years ago, fri, august 26, 2011, 12:00:00

How do I check whether a certain div exist on my page and if not, redirect the visitor to another page?


More From » html

 Answers
89

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;
}
});​

[#90400] Thursday, August 25, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emilianoc

Total Points: 568
Total Questions: 109
Total Answers: 99

Location: Oman
Member since Sat, Jan 7, 2023
1 Year ago
;