Talk:Code snippets

From GreaseSpot Wiki
Revision as of 22:18, 25 March 2007 by 81.225.78.185 (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

array.forEach in XPath example

Re: http://wiki.greasespot.net/index.php?title=Code_Snippets&diff=prev&oldid=1784

Restored that example, it works fine (using Fx 2) without Prototype. See e.g. http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach.

Just try it:

 // ==UserScript==
 // @name          Example
 // @namespace     wiki
 // @include       *
 // ==/UserScript==
 
 function $x(p, context) {
   if (!context) context = document;
   var i, arr = [], xpr = document.evaluate(p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
   for (i = 0; item = xpr.snapshotItem(i); i++) arr.push(item);
   return arr;
 }
 
 var i, paragraphs = $x("//p");	 
 paragraphs.forEach(function(paragraph) { // Loop over every paragraph	 
 	paragraph.innerHTML = "Halloa!";	 
 });

--81.225.78.185 18:18, 25 March 2007 (EDT)