Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > removeNode() for a dynamic div doesn't work in firefox

Reply
Thread Tools

removeNode() for a dynamic div doesn't work in firefox

 
 
Tim Mackey
Guest
Posts: n/a
 
      12-19-2004
hi,
i have a javascript function to highlight google search keywords in the
page. it works well on IE and mozilla browsers. for the page OnLoad, i
call the Highlight() method, and that highlights the words in the page, and
inserts a div element with the message: "your search terms have been
highlighted..." and a link to remove the highlighting, which has
href='javascript:removeHighlight(..)', but that only works in IE, not
firefox 1.0.

am i using an IE-only javascript feature? the code from the javascript file
is below, and if you'd like to see it in action, go to
http://www.google.ie/search?num=100&...software+iserc and
click the first link (at time of writing it is www.iserc.ie/diary/2004/
november5isercworkshopadaptivesoftware/) . you need to go through google to
get the referrer. you should see the message at the top when the page
loads, and clicking the link should remove the highlighting and message.

i've done as much debugging as i can, and the RemoveHighlight function is
executing, and it does return the HighlightMsg div with the getElementByID
function correctly. it's just that RemoveNode does nothing to it for
firefox, but it works in IE.

thanks for any help
tim mackey. code below.

function Highlight(element)
{
...
//Replace search words
var msg = "<div id='HighlightMsg'>Your search terms have been highlighted:
";
var color = 0;
var max = 5;
for(j=0; j<aWords.length; j++)
{
regexp= new RegExp ("(" + aWords[j] + ")", "gi");
vStrippedHTML = vStrippedHTML.replace(regexp,'<span class="hl' + color+
'">$1</span>');
// build up message notifying user of highlighting
msg = msg + "<span class='hl" + color+ "'>" + aWords[j] + "</span> ";
color++;
}
//Reinsert HTML
for(i=0;vStrippedHTML.indexOf("$!$") > -1;i++)
vStrippedHTML = vStrippedHTML.replace("$!$", vHTMLArray[i]);
msg = msg + " - <a
href='javascript:RemoveHighlight(document.getEleme ntById(\"HTML\"))'>Remove
Highlighting</a></div>";
element.innerHTML = msg + vStrippedHTML;
}

function RemoveHighlight(element)
{
document.getElementById("HighlightMsg").removeNode (true);
regexp=/(<span class\=hl\d>)([^<>]*)(<\/span>)/ig;
element.innerHTML = element.innerHTML.replace(regexp, "$2");
}
window.onload=function(){Highlight(document.getEle mentById("HTML"));};




\\ email: tim at mackey dot ie //
\\ blog: http://tim.mackey.ie //
67d0ebfec70e8db3


 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      12-19-2004


Tim Mackey wrote:



> a link to remove the highlighting, which has
> href='javascript:removeHighlight(..)', but that only works in IE, not
> firefox 1.0.



> function RemoveHighlight(element)
> {
> document.getElementById("HighlightMsg").removeNode (true);


Mozilla doesn't know a removeNode method, you probably want
var el = document.getElementById("HighlightMsg");
el.parentNode.removeChild(el);

> regexp=/(<span class\=hl\d>)([^<>]*)(<\/span>)/ig;
> element.innerHTML = element.innerHTML.replace(regexp, "$2");


I haven't looked at that deeply but innerHTML manipulation is a gamble,
it seems your regular expression expects the class attribute value to
not be quoted, IE might give innerHTML that way, but Mozilla might have
the value quoted so you need to write a regular expression catering to
both ways.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      12-19-2004


Martin Honnen wrote:


> it seems your regular expression expects the class attribute value to
> not be quoted, IE might give innerHTML that way, but Mozilla might have
> the value quoted so you need to write a regular expression catering to
> both ways.


The following regular expression might do that:
var regExp = /<span class=("?)h1\d+\1>([^<>]*)<\/span>/gi;
but I haven't tried to intergrate it in your innerHTML manipulation code.


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
Tim Mackey
Guest
Posts: n/a
 
      12-19-2004
martin you are a genius. i implemented both your suggestions and it works
perfectly now. thanks!

for anyone who wants to use the code, (it works quite nicely because there
is no server side processing), i have included the updated version below.
just save it into a .js file, and include it in any pages that you want to
support keyword highlighting.

function Highlight(element)
{
var aWords;
var ref = document.referrer;
if(ref == null || ref == "")
return;
var sets = ref.split('&');
for(i=0; i<sets.length; i++)
if(sets[i].indexOf("q=") > -1)
aWords = sets[i].split('=')[1].split('+');
if(aWords == null || aWords.length == 0)
return;
//Extract HTML Tags
regexp=/<[^<>]*>/ig;
vHTMLArray = element.innerHTML.match(regexp);
//Replace HTML tags
vStrippedHTML = element.innerHTML.replace(regexp,"$!$");

//Replace search words
var msg = "<div id='HighlightMsg'>Your search terms have been highlighted:
";
var color = 0;
var max = 5;
for(j=0; j<aWords.length; j++)
{
regexp= new RegExp ("(" + aWords[j] + ")", "gi");
vStrippedHTML = vStrippedHTML.replace(regexp,'<span class="hl' + color+
'">$1</span>');
// build up message notifying user of highlighting
msg = msg + "<span class='hl" + color+ "'>" + aWords[j] + "</span> ";
color++;
}
//Reinsert HTML
for(i=0;vStrippedHTML.indexOf("$!$") > -1;i++)
vStrippedHTML = vStrippedHTML.replace("$!$", vHTMLArray[i]);
msg = msg + " - <a
href='javascript:RemoveHighlight(document.getEleme ntById(\"HTML\"))'>Remove
Highlighting</a></div>";
element.innerHTML = msg + vStrippedHTML;
}

function RemoveHighlight(element)
{
var msgDiv = document.getElementById("HighlightMsg");
msgDiv.parentNode.removeChild(msgDiv);
regexp=/(<span class\="?hl\d"?>)([^<>]*)(<\/span>)/ig;
element.innerHTML = element.innerHTML.replace(regexp, "$2");
}
window.onload=function(){Highlight(document.getEle mentById("HTML"));};



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic/javascript: div in div = 3 divs..!? .rhavin grobert Javascript 11 11-20-2009 08:39 PM
<div ... /> and <div ...></div> K Viltersten ASP .Net 4 03-31-2009 07:33 PM
TTo o position a div after a dynamic div H.Steel Javascript 1 03-14-2008 12:00 PM
NS/FF don't change div offsetWidth when div innerHTML is added toand div becomes wider mscir Javascript 3 06-26-2005 04:04 PM
Q: Div A inside Div B is larger than Div B Dwayne Madsen Javascript 1 06-01-2005 03:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57