Page 1 of 1

possible to parse a html page for div id's ...

Posted: Mon Oct 26, 2009 7:10 pm
by krraleigh
Is it possible to parse a web page and capture the values of all the div id elements and store them into
an array obj if the match a reg expression of say /^F/d+$/ so that I can act on them depending on what they are?

Thanx
Kevin

Re: possible to parse a html page for div id's ...

Posted: Tue Oct 27, 2009 12:37 pm
by kaszu
One way is to take document.body.innerHTML and find all id=... , another way is to traverse all elements and check if they have ID's (or document.querySelectorAll('*[id]') if it's supported) and match against regular expression.
Here is second way:

Code: Select all

var els = [];
var ids = [];
 
//Get list of all elements (if possible only those which have IDs)
if (typeof document.querySelectorAll == 'undefined') {
    var els = document.getElementsByTagName('*');
} else {
    var els = document.querySelectorAll('*[id]');
}
 
for(var i=0,j=els.length; i<j; i++) {
    //Check if element has an ID and if it does, then match against your pattern
    if (els[i].id && els[i].id.match(/^F\d+$/)) {
        ids.push(els[i].id);
    }
}
 
alert(ids.length);
It was a lot faster in FF, which supports querySelectorAll, don't know how well it will do in IE.