943,169 Members | Top Members by Rank

Ad:
Aug 31st, 2010
0

unresponsive script

Expand Post »
Hi all,

The following script is a task I set myself to learn javascript, essentially there are several arrays containg keywords which, depending on the array, should highlight those words in different colours.

The code works but I get an unresponsive script error and it's very slow. I think because of the document.body innerhtml part as I tried keyword swaps and it was fine.

javascript Syntax (Toggle Plain Text)
  1. var textnodes = document.evaluate( "//body//text()", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  2.  
  3. for (var i = 0; i < textnodes.snapshotLength; i++)
  4. {
  5. node = textnodes.snapshotItem(i);
  6. node.data = highlight(node.data);
  7. }
  8.  
  9. function highlight()
  10. {
  11. for (var j = 0; j < testArrayOne.length; j++)
  12. {
  13. document.body.innerHTML = document.body.innerHTML.replace(new RegExp(testArrayOne[j]),'<span style="color:#ff0000">' + testArrayOne[j] + '</span>');
  14. }
  15.  
  16. for (var j = 0; j < testArrayTwo.length; j++)
  17. {
  18. document.body.innerHTML = document.body.innerHTML.replace(new RegExp(testArrayTwo[j]),'<span style="color:#FF00FF">' + testArrayTwo[j] + '</span>');
  19. }
  20.  
  21. }

Thanks for your help.

P.S. this is a userscript for greasemonkey
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cotillion is offline Offline
3 posts
since Jul 2010
Sep 1st, 2010
0
Re: unresponsive script
use string replace instead of regex
Reputation Points: 6
Solved Threads: 19
Posting Whiz in Training
fatihpiristine is offline Offline
283 posts
since Sep 2007
Sep 6th, 2010
0
Re: unresponsive script
use string replace instead of regex
Thanks for the reply.

I assumed you meant like this;

javascript Syntax (Toggle Plain Text)
  1. document.body.innerHTML = document.body.innerHTML.replace(String.replace(testArrayOne[j]),'<span style="color:#ff0000">' + testArrayOne[j] + '</span>');

which is still slow. any thoughts?

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cotillion is offline Offline
3 posts
since Jul 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Toolbar
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Passing data from a form on one page to another form on another page





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC