Skip to content

Instantly share code, notes, and snippets.

@Yogu
Created February 7, 2014 13:54
Show Gist options
  • Save Yogu/8862953 to your computer and use it in GitHub Desktop.
Save Yogu/8862953 to your computer and use it in GitHub Desktop.
Recursively find string in DOM
function find(object, search, cache, path) {
if (!cache)
cache = [];
if (!path)
path = '';
if (cache.indexOf(object) >= 0)
return;
cache.push(object);
for (var key in object) {
if (object.hasOwnProperty && !object.hasOwnProperty(key))
continue;
var found = key.indexOf(search) >= 0;
var value;
try {
value = object[key];
} catch (e) {}
found |= value && value.indexOf && value.indexOf(search) >= 0;
if (found) {
console.log(path);
console.log(object);
}
if (value instanceof Object)
find(value, search, cache, path + '.' + key);
}
}
find(window, 'string to search in the entire DOM')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment