javascript if statent

D

Dan

Hi

I am trying to use this javascript code to see if a frame is part of a
parent. It displays yes when it is part of a parent, but my only
problem is that it doesn't display no when it is not part of a parent.
In other words, when top.bob.document.title = test it displays yes, but
when it isn't test it doesn't display anything.

<script>
if (top.bob.document.title == "test"){
document.write("yes")
}
else{
document.write("no")
}
</script>

Dan
 
R

RobG

Dan said:
Hi

I am trying to use this javascript code to see if a frame is part of a
parent. It displays yes when it is part of a parent, but my only
problem is that it doesn't display no when it is not part of a parent.
In other words, when top.bob.document.title = test it displays yes, but
when it isn't test it doesn't display anything.

Because if top.bob doesn't exist, then attempting top.bob.document will
cause an error and the script won't run. The JavaScript console will
have a message like:

'top.bob has no properties'


The type attribute is required:

if (top.bob.document.title == "test"){

if (top
&& top.bob
&& tob.bob.document.title
&& tob.bob.document.title == 'test'){


[...]
 
D

Dan

Okay now I have the following code but it doesn't display anything if
it is part of a parent:

<script type="text/javascript">
if (top
&& top.bob
&& tob.bob.document.title
&& tob.bob.document.title == "test"){
document.write("yes")
}
else{
document.write("no")
}
</script>
 
R

RobG

Dan said:
Okay now I have the following code but it doesn't display anything if
it is part of a parent:

I made a mess of it - look for 'tob', sheesh - early Monday ain't my
best time of the day. Forget the other post, use the stuff below.

If the script is in a document loaded into a frame, then:


<script type="text/javascript">

var o;
if ( (o = top)
&& (o = o.frames)
&& (o = o.bob)
&& (o = o.document)
&& (o = o.title)
&& o == "test"
){
document.write("yes")
} else {
document.write("no")
}
</script>



But if the script is in 'top', you can't get access to the document in
the iFrame until after the frame has loaded, so use an onload function:

<script type="text/javascript">

window.onload = function(){
var o;
if ( (o = top)
&& (o = o.frames)
&& (o = o.bob)
&& (o = o.document)
&& (o = o.title)
&& o == "test"
){
alert('bob and test found');
} else {
alert('bob or test not found');
}
}

</script>

[...]


And remember cross-domain issues with getting access to the iFrame content.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top