473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help with script optimization

YenRaven
I have wrote a script that searches through the body's innerHTML to find and highlight each instance string omiting any matches in html tags. it works.. but slowly to the point that if you have more than about 100 matches it hangs up and crashes. I have tested the script in FireFox 1.5 only so i dont know how well it will work in IE. I was wondering if anyone knows any ways I could optimize this script to get it to run smoother. Here it is brace yourself...

function searchString(){
if(document.getElementById("search").value != ""){ //Check to see if there is a value in the search bar

var currIndex = 0
var searchText = document.getElementById("search").value.toLowerCas e(); //grab serach string from search bar.

do {
var bodyHTML = document.body.innerHTML //grab a copy of html so we dont return any formated highlighted text
var bodytext = document.body.innerHTML.toLowerCase(); //grab a formated copy of the html for case insentive searching.
if((bodytext.indexOf("<", bodytext.indexOf(searchText, currIndex)) < bodytext.indexOf(">", bodytext.indexOf(searchText, currIndex)) || bodytext.lastIndexOf(">", bodytext.indexOf(searchText, currIndex)) > bodytext.lastIndexOf("<", bodytext.indexOf(searchText, currIndex))) && (bodytext.charAt(bodytext.indexOf(searchText, currIndex)) != "<" && bodytext.charAt(bodytext.indexOf(searchText, currIndex)) != ">") && bodytext.indexOf(searchText, currIndex) > -1){ //This massive if statement checks to see if the next instance of the search string is inside a html statement.

var replaceString = "<font style='background-color:#ffff00;'>"+ bodyHTML.substr(bodytext.indexOf(searchText, currIndex), searchText.length) +"</font>"; //compiles the string to replace the found string with.
document.body.innerHTML = bodyHTML.substring(0, bodytext.indexOf(searchText, currIndex))+ replaceString +bodyHTML.substr(bodytext.indexOf(searchText, currIndex) + searchText.length) //replace the string.

currIndex = bodytext.indexOf(searchText, currIndex) + replaceString.length; //move the index in front of the last instance found.
}else{
currIndex = bodytext.indexOf(searchText, currIndex) + 1 //move the index beyond the last instance found.
}
}while(document.body.innerHTML.toLowerCase().index Of(searchText, currIndex) > -1); //do while the search string exists beyond the current index.
}
}
Oct 17 '06 #1
1 1541
sry im a dufus this might look a little better.

Expand|Select|Wrap|Line Numbers
  1. function searchString(){
  2. if(document.getElementById("search").value != ""){ //Check to see if there is a value in the search bar
  3.  
  4. var currIndex = 0
  5. var searchText = document.getElementById("search").value.toLowerCase(); //grab serach string from search bar.
  6.  
  7. do {
  8. var bodyHTML = document.body.innerHTML //grab a copy of html so we dont return any formated highlighted text
  9. var bodytext = document.body.innerHTML.toLowerCase(); //grab a formated copy of the html for case insentive searching.
  10. if((bodytext.indexOf("<", bodytext.indexOf(searchText, currIndex)) < bodytext.indexOf(">", bodytext.indexOf(searchText, currIndex)) || bodytext.lastIndexOf(">", bodytext.indexOf(searchText, currIndex)) > bodytext.lastIndexOf("<", bodytext.indexOf(searchText, currIndex))) && (bodytext.charAt(bodytext.indexOf(searchText, currIndex)) != "<" && bodytext.charAt(bodytext.indexOf(searchText, currIndex)) != ">") && bodytext.indexOf(searchText, currIndex) > -1){ //This massive if statement checks to see if the next instance of the search string is inside a html statement.
  11.  
  12. var replaceString = "<font style='background-color:#ffff00;'>"+ bodyHTML.substr(bodytext.indexOf(searchText, currIndex), searchText.length) +"</font>"; //compiles the string to replace the found string with.
  13. document.body.innerHTML = bodyHTML.substring(0, bodytext.indexOf(searchText, currIndex))+ replaceString +bodyHTML.substr(bodytext.indexOf(searchText, currIndex) + searchText.length) //replace the string.
  14.  
  15. currIndex = bodytext.indexOf(searchText, currIndex) + replaceString.length; //move the index in front of the last instance found.
  16. }else{
  17. currIndex = bodytext.indexOf(searchText, currIndex) + 1 //move the index beyond the last instance found.
  18. }
  19. }while(document.body.innerHTML.toLowerCase().index Of(searchText, currIndex) > -1); //do while the search string exists beyond the current index.
  20. }
  21. }
Oct 17 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: WantedToBeDBA | last post by:
Hi all, db2 => create table emp(empno int not null primary key, \ db2 (cont.) => sex char(1) not null constraint s_check check \ db2 (cont.) => (sex in ('m','f')) \ db2 (cont.) => not enforced...
1
by: Daniel Fitzgerald | last post by:
Hi all, I need some pointers. I want to optimize my pgsql databases hardware-wise. I can do this in Sybase, Oracle, SQL Server, etc. by putting let's say, indexes on one device, the trasaction logs...
2
by: Beemer Biker | last post by:
I have a 10 row (per page) gridview where the first column is a small image under 100x200px and the second column is a DropDownList template that selects which image to show in the first column. ...
1
by: David | last post by:
It's too long to post here (160 lines) so here's the link: Neither link works for me. I get an error page "Error: invalid download linnk". How about you send it to the list as an attachment? ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.