views:

55

answers:

2

Hi everybody,

I'm using this function that helps my webdevelopers to find a piece of code in a webpage:

function findString (str) {
  var strFound;
  strFound=self.find(str);
  if (strFound && self.getSelection && !self.getSelection().anchorNode ){   
    strFound=self.find(str);
  }
  if (!strFound) {
    strFound=self.find(str,1,1)
    while (self.find(str,1,1)) continue
  }
}

The problem is that when I enter the following string for example:

array[1]

It can't find it! Which is strange because I tried this function and It can find any other string. So, how can I use this function to find a string containing square brackets (without using regular expressions if it's possible)?

Thank you,

Regards

A: 

What object function find is called on? Is that a window? Is yes, that what exactly does the function find? According to documentation it is:

The find() method displays a find dialog box when invoked. This allows a user to search for a string in the page from which it was invoked.

Does that mean that HTML tokens are not processed and you're exactly looking for HTML token array[1]? Which browsers is the function supported in?

One way to implement server-side search is to read the whole file (assuming you know the location of the file) and do a simple regular expression using string function search

Leonid
I'm using Firefox 3.6.8. You are right, the search function is helpful, but the find function not only finds the string, but also scroll into it and hightlight it. that's why I'm trying to get it working with the find function.
Zakaria
A: 

I tried you script and there is a bug in it. If you replace the following

if (strFound && self.getSelection && !self.getSelection().anchorNode )  

by the following

if (strFound && self.getSelection && !self.getSelection().anchorNode )  {

it works.

In Firefox and Chrome, strFound is true when the searched string is array[1]. In IE 8, script doesn't work.

EDIT: code I've tested:

$(document).ready(function() {
    findString("array[1]");
});

function findString (str) {
    var strFound;
    strFound=self.find(str);
    if (strFound && self.getSelection && !self.getSelection().anchorNode )  {
        strFound=self.find(str);
    }
    if (!strFound) {
        strFound=self.find(str,1,1)
        while (self.find(str,1,1)) continue
    }
    alert(strFound);
 }
Zafer
I edited the post. I forgot the opening bracket but even that, it didn't work. Can you "pastebin" your code please? Thank you.
Zakaria
function findString (str) { var strFound; strFound=self.find(str); if (strFound } if (!strFound) { strFound=self.find(str,1,1) while (self.find(str,1,1)) continue } alert(strFound); } $(document).ready(function() { findString("array[1]"); });
Zafer