Forum Moderators: open

Message Too Old, No Replies

Executing function only when in frames?

Heres the code, why is it executing when in frames though?

         

JAB Creations

2:27 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to allow people to view my site without frames when the next version comes out and so there are some scripts that need to be ignored and do nothing if the script detects the page is not inside of a frame.

This script however still executes (clicking every four times will lead Firefox to declare it can not find "parent.border" which is the frame called border. I'm trying to keep it from executing that bit in the first place when there are no frames (to keep from having any JavaScript errors).

Here is the code, what am I doing wrong here?

//<![CDATA[
if (top.location!=self.location)
{
var clickCount = 0;
var clickSpacing = 4;
var clickCycle = 22;
document.onclick = function()
{
clickCount = ++clickCount % clickCycle // if 22, return to zero
if(clickCount &&!(clickCount % clickSpacing)) // if clickCount is non-zero multiple of clickSpacing
parent.border.location
= 'http://example.com/border'
+ (clickCount/clickSpacing)
+ '.php';
}
}
else if (top.location==self.location)
{
return false;
}
////]]>

JAB Creations

5:31 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here are some ways I'm trying to reference this...I know I'm lacking the correct terminology...

if (parent.border)

if (parent.border!= 'undefined')

if (parent.border!= 1)

None of these seem to work as the script keeps executing if the frame does not exist. I am of course testing this script on a page that is not currently in a frameset and so there is no frame called "border". The same should (not) happen if I was testing in frames but none of the frames had a name of "border".

So I'm still stuck on this challenge.

John

JAB Creations

6:17 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok I have it working BUT the only problem is it works correctly when internal on the page (within the head element) but fails when the script is called from an external file. Why? Here is the script...

<script type="text/javascript">
//<![CDATA[
if (parent.border)
{
var clickCount = 0;
var clickSpacing = 4;
var clickCycle = 22;
document.onclick = function()
{
clickCount = ++clickCount % clickCycle // if 22, return to zero
if(clickCount &&!(clickCount % clickSpacing)) // if clickCount is non-zero multiple of clickSpacing
parent.border.location
= 'http://example.com/border'
+ (clickCount/clickSpacing)
+ '.php';
}
}
else
if (parent.border!= 'undefined')
{
alert('undefined');
}
////]]>
</script>

JAB Creations

6:44 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SOLVED!

To add a restrictive condition to an already existing script that is located in an external file all that has to be done is to check if the object autocasts to true...is that the correct terminology btw?

Thankfully this is all I had to add around the original script...

if (parent.border)
{
}

Finally the script that works only if a frame with the name "border" exists that produces no errors while serving application/xhtml+xml and as an external file IoI...

Here is the combined script that appears in the external file now...

//<![CDATA[
if (parent.border)
{
var clickCount = 0;
var clickSpacing = 4;
var clickCycle = 22;
document.onclick = function()
{
clickCount = ++clickCount % clickCycle // if 22, return to zero
if(clickCount &&!(clickCount % clickSpacing)) // if clickCount is non-zero multiple of clickSpacing
parent.border.location
= 'http://example.com/border'
+ (clickCount/clickSpacing)
+ '.php';
}
}
////]]>

Hopefully someone else will find my wandering in the dark useful somehow! :)

John