WebKit adds getElementsByClassName
Posted on 22nd December 2007 in Browsers, Open Source Software, Open Standards | 2 Comments »
WebKit now natively supports getElementsByClassName, one of the most requested functions by JavaScript programmers.
Function getElementsByClassName
Parameters
String – String name of the Stylesheet class
Returns
Array of objects matched
Here’s a simple test:
JavaScript for the above sample is following:
function changeColour() { if (!document.getElementsByClassName) { if (confirm("You need a compatible browser to run this test!")) { document.location.href = "http://nightly.webkit.org/"; } } else { var elems = document.getElementsByClassName("testdiv"); elems[0].style.backgroundColor = "#CCCCCC"; elems[0].style.color = "#000000"; } } function resetColour() { if (!document.getElementsByClassName) { if (confirm("You need a compatible browser to run this test!")) { document.location.href = "http://nightly.webkit.org/"; } } else { var elems = document.getElementsByClassName("testdiv"); elems[0].style.backgroundColor = "#FF0000"; elems[0].style.color = "#ffffff"; } } |
For those interested WebKit developers have also put up a benchmark to illustrate how superior native support is over any other possible solution.
NB! To test this functionality you need to download recent nightly of WebKit.


2 Responses
FYI: This also works in Opera 9.5 beta builds and Firefox nightlies.
Yes, the above sample is also tested to work on Firefox 3 Beta 2 that Mozilla released just recently.