Created
March 25, 2014 00:31
-
-
Save bennadel/9752500 to your computer and use it in GitHub Desktop.
Javascript Will Assign And Test A Variable In The Same Statement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get the page header. | |
var objHeader = document.getElementById( "header" ); | |
// Check to see if the header exists. | |
if (objHeader){ | |
... more code here ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- HTLM header / span. --> | |
<div id="header"><span>Title Span</span></div> | |
<script type="text/javascript"> | |
// Define the variables as null. | |
var objHeader = null; | |
var objHeaderSpan = null; | |
// In this IF statement, assign the variables and | |
// check to see if they result in valid objects. | |
if ( | |
(objHeader = document.getElementById( "header" )) && | |
(objHeaderSpan = objHeader.childNodes[ 0 ]) | |
){ | |
// If we have gotten this far, then we have assigned | |
// DOM elements to both objHeader and objHeaderSpan | |
// and we KNOW that they exist. Aler inner HTML. | |
alert( objHeaderSpan.innerHTML ); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment