Created
March 16, 2015 00:10
-
-
Save claycarpenter/f5c0b525c86df00073a8 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/nolaqe
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<h1>Lorem Ipsum Title Sit Amet</h1> | |
<p>Support collaborative catalyst Rosa Parks; criteria; compassion; social entrepreneurship citizenry change movements. Revitalize opportunity insurmountable challenges stakeholders cause institutions, healthcare; Arab Spring enabler. Asylum combat poverty cooperation, implementation research proper resources human experience sustainable future educate partnership The Elders. | |
</p> | |
<script id="jsbin-javascript"> | |
function containsText(node, string) { | |
if (node.nodeType == document.ELEMENT_NODE) { | |
// Continue to recurse down the tree to each descendant | |
// node, looking for text nodes that might contain the | |
// search text. | |
for (var i = 0, x = node.childNodes.length; i < x; i++) { | |
var childNode = node.childNodes[i]; | |
if (containsText(childNode, string)) { | |
return true; | |
} | |
} | |
return false; | |
} else if (node.nodeType == document.TEXT_NODE) { | |
// Return true if the search string is contained within | |
// the text node. | |
if (node.nodeValue.indexOf(string) > -1) { | |
console.log('Found search string "' + string + '" in text node "' + node.nodeValue + '"'); | |
} | |
return node.nodeValue.indexOf(string) > -1; | |
} | |
} | |
var titleHeader = document.getElementsByTagName('h1')[0]; | |
var searchResult = containsText(titleHeader, 'Lorem'); | |
console.log('Title contains text "Lorem":', searchResult); | |
var searchResult = containsText(titleHeader, 'Dolor'); | |
console.log('Title contains text "Dolor":', searchResult); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
function containsText(node, string) { | |
if (node.nodeType == document.ELEMENT_NODE) { | |
// Continue to recurse down the tree to each descendant | |
// node, looking for text nodes that might contain the | |
// search text. | |
for (var i = 0, x = node.childNodes.length; i < x; i++) { | |
var childNode = node.childNodes[i]; | |
if (containsText(childNode, string)) { | |
return true; | |
} | |
} | |
return false; | |
} else if (node.nodeType == document.TEXT_NODE) { | |
// Return true if the search string is contained within | |
// the text node. | |
if (node.nodeValue.indexOf(string) > -1) { | |
console.log('Found search string "' + string + '" in text node "' + node.nodeValue + '"'); | |
} | |
return node.nodeValue.indexOf(string) > -1; | |
} | |
} | |
var titleHeader = document.getElementsByTagName('h1')[0]; | |
var searchResult = containsText(titleHeader, 'Lorem'); | |
console.log('Title contains text "Lorem":', searchResult); | |
var searchResult = containsText(titleHeader, 'Dolor'); | |
console.log('Title contains text "Dolor":', searchResult); | |
</script></body> | |
</html> |
This file contains 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
function containsText(node, string) { | |
if (node.nodeType == document.ELEMENT_NODE) { | |
// Continue to recurse down the tree to each descendant | |
// node, looking for text nodes that might contain the | |
// search text. | |
for (var i = 0, x = node.childNodes.length; i < x; i++) { | |
var childNode = node.childNodes[i]; | |
if (containsText(childNode, string)) { | |
return true; | |
} | |
} | |
return false; | |
} else if (node.nodeType == document.TEXT_NODE) { | |
// Return true if the search string is contained within | |
// the text node. | |
if (node.nodeValue.indexOf(string) > -1) { | |
console.log('Found search string "' + string + '" in text node "' + node.nodeValue + '"'); | |
} | |
return node.nodeValue.indexOf(string) > -1; | |
} | |
} | |
var titleHeader = document.getElementsByTagName('h1')[0]; | |
var searchResult = containsText(titleHeader, 'Lorem'); | |
console.log('Title contains text "Lorem":', searchResult); | |
var searchResult = containsText(titleHeader, 'Dolor'); | |
console.log('Title contains text "Dolor":', searchResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment