here is the html code :
<div id="popup" class="popup_block" >
<img src="images/PUB-histRIRE.jpg" alt="popup" />
</div>
and script :
<script type="text/javascript">
$(document).ready(function(){
popWidth = 690;
popHeight = 550;
popID = 'popup';
var popMargTop = popHeight / 2;
var popMargLeft = popWidth / 2;
//Apply Margin to Popup
$('#' + popID).css({
'width': popWidth,
'height': popHeight,
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft,
'visibility' : 'visible'
});
//Fade in Background
$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer
//Close Popups and Fade Layer
$('a.close, #fade, .popup_block').live('click', function() { //When clicking on the close or fade layer...
$('#fade , .popup_block').fadeOut(); //fade them both out
$('#fade').remove();
return false;
});
});
</script>
I like to execute the code only on page with the div... on page without thi div, just ignore. I can make a if by parsin the URL... but it look more complicated to me... any simple jquery trick ?
In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements. To check if an element which has an id of “div1” exists.
The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)
Using driver. In order to check if an element is present on a webpage, we make use of driver. findElements() method. As we know that driver. findElements() method returns a list of webElements located by the “By Locator” passed as parameter.
if($('#popup').length >0 ){
//your code here
}
The way to do this is (or was) to check the length property like so:
if ($("#"+ popID).length > 0){
// do stuff
}
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