Forum Moderators: open

Message Too Old, No Replies

Determining if span/div text is visible or not

         

ChrisMcC

11:05 pm on Jun 17, 2004 (gmt 0)

10+ Year Member



Hi--

Does anyone know a simple way to determine if a snippet of text curently is hidden? In other words, is the text contained within a collpased <div> or <span>?

I have a local-page search algorithm that highlights (selects) a search word or phrase. Unfortunately if the search word or phrase is within a hidden <div> or <span>, then the select() statement that accomplishes the highlighting, generates a runtime error. In other words, if the search word or phrase is contained within a <div style=display:none> or a <span style=display:none>, then the select() code crashes. If the <div> or <span> is expanded (visible), then all is well; however, expanding everything before searching defeats the purpose of the collapsed text.

I know that one approach is something along the lines of

if(textRange.parentNode.style.display=="none")

but this does not address the situation where the hidden text sometimes is buried inside more tags, so the parentNode may not be appropiate. It seems complicated to do recursion, and I just thought there might be an easy, more generalized way.

Thank you for any ideas!

Chris

Purple Martin

11:33 pm on Jun 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to do it the recursive way. You could use a while loop to crawl back up until you reach the body tag (you could give the body tag an id to make it easy to tell when you've reached it).

ChrisMcC

12:02 am on Jun 18, 2004 (gmt 0)

10+ Year Member



I hate to ask this, but would you mind showing me the way? I'm afraid that I'm not very good with JavaScript...

Here's the routine that crashes when is hits str = [hidden text].


TRange = document.body.createTextRange();
strFound = TRange.findText(str);
if (strFound) {TRange.select();}

Bernard Marx

1:11 am on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not suffering similar crashes with hidden text, but I'll press on anyway. I can't think of any other solution than recursion, but it's not too tricky. I'm assuming that this is for IE only. I don't know whether the others have good support for text ranges yet. I say this because there is one element that is IE proprietary:

I couldn't get the parent element of a text range other than to use the IE method,

[blue]parentElement()[/blue]
.

First, as Martin says, we need a recursive function that climbs up though the ancestry, and returns

[blue]false[/blue]
if it hits an element with
[blue]display:none[/blue]
. It returns
[blue]true[/blue]
if it gets to the BODY.

It receives the parent element as an argument.

function isDisplayed(parent)
{
var body = document.body
while(parent!= body)
{
if(parent.style.display == "none")
return false
parent = parent.parentNode
}
return true
}

Then you check to see if your text is displayed like this:

if( isDisplayed(TRange.parentElement() )

ChrisMcC

1:55 am on Jun 18, 2004 (gmt 0)

10+ Year Member



That was a HUGE help! It worked great, and you cannot imagine how many hours (days?) it saved me.

I've confirmed the wierdness with respect to errors on attempting to select hidden text. It could be because our site is hosted on a somewhat unusual server. As another example of different server behavior, we know that URLs addressed to our server are case-sensitive.

Thank you very much again for the great bread crumbs!

Chris

Rambo Tribble

2:24 am on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just wondering, what if the node of concern has visibility:hidden instead of display:none?

Purple Martin

4:37 am on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just wondering, what if the node of concern has visibility:hidden instead of display:none?

if(parent.style.display == "none" ¦¦ parent.style.visibility == "hidden")

;)

ChrisMcC

4:03 pm on Jun 18, 2004 (gmt 0)

10+ Year Member



I'm searching a frame (Main), so "parent" got a bit dicey. Here's what I ended up with:


function isDisplayed(parentElement) {
var body=parent.Main.document.body;
while(parentElement!= body) {
if(parentElement.style.display=="none" ¦¦ parentElement.style.visibility=="hidden" ) {
return false;
}
else {
parentElement=parentElement.parentNode;
}
}
return true;
}

Bernard Marx

4:23 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Chris, as regards #5. The server will have no effect on DHTML behaviour, as it's all client-side (by defn). (No server expert but) case-sensitivity with urls depends on the OS being used for the server. Very, very loosely, Unix-based OS's are case-sensitive for pathnames, and Windows isn't.

I don't know the figures either, but it's quite common to be creating a site on a Windows PC, then uploading it to Apache, with the resulting case problems. I was taught not to use upper-case for filenames ... ever, and this was before I knew the difference between a web server and a nice hot cup of tea, so I don't often get this.