Dynamic IFRAME problem

T

Thomas 'PointedEars' Lahn

[...] I need to integrate my code with an external script.
The external script is using document.write(<iframe...)

and my script should decide if to write the external script..

document.write("<script...>") (which writes an Iframe element)

This is almost always nonsense. Why do you not include the generated
`script' element in the first place?
OR
write my Iframe

document.write("<iframe...")

so I need to stick with this logic. [...]

No, you don't. As Ann (Giggle Girl) pointed out already, you can modify the
`iframe' element generated by the "external script" afterwards. Which
should be reliable on cached reload in contrast to your efforts of cheating
the cache somehow.

<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function modifySrc()
{
if (typeof window != "undefined"
&& window.frames
&& window.frames[0]
&& Math.random() > 0.5)
{
window.frames[0].src = "http://www.yahoo.com/";
}
}
</script>
</head>

<body onload="modifySrc()">
...
<!-- "external script" that generates an
iframe[src="[URL]http://www.google.com/[/URL]"] element;
let it be the only one here for simplicity of
the example -->
<script src="..." type="text/javascript"></script>
...
</body>


PointedEars
 
S

shlomi.schwartz

Thomas said:
[...] I need to integrate my code with an external script.
The external script is using document.write(<iframe...)

and my script should decide if to write the external script..

document.write("<script...>") (which writes an Iframe element)

This is almost always nonsense. Why do you not include the generated
`script' element in the first place?
OR
write my Iframe

document.write("<iframe...")

so I need to stick with this logic. [...]

No, you don't. As Ann (Giggle Girl) pointed out already, you can modify the
`iframe' element generated by the "external script" afterwards. Which
should be reliable on cached reload in contrast to your efforts of cheating
the cache somehow.

<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function modifySrc()
{
if (typeof window != "undefined"
&& window.frames
&& window.frames[0]
&& Math.random() > 0.5)
{
window.frames[0].src = "http://www.yahoo.com/";
}
}
</script>
</head>

<body onload="modifySrc()">
...
<!-- "external script" that generates an
iframe[src="[URL]http://www.google.com/[/URL]"] element;
let it be the only one here for simplicity of
the example -->
<script src="..." type="text/javascript"></script>
...
</body>


PointedEars

There is a fundamental error in this flow.
If the script is "Hard-coded" to the body the result would be like so:

Lets say an ad is displaying in the "script iframe"
the page loads and writes the "Script Iframe" therefore creating a
"server hit" to the ad publisher, resolving with a cache request (I
mean money) for their service...
then the page loads and the iframe src is changed to my page.

the correct flow would be to load the correct Iframe from the beginning
 
T

Thomas 'PointedEars' Lahn

Thomas said:
(e-mail address removed) wrote:
[...]
No, you don't. As Ann (Giggle Girl) pointed out already, you can modify
the `iframe' element generated by the "external script" afterwards.
Which should be reliable on cached reload in contrast to your efforts of
cheating the cache somehow.

<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function modifySrc()
{
if (typeof window != "undefined"
&& window.frames
&& window.frames[0]
&& Math.random() > 0.5)
{
window.frames[0].src = "http://www.yahoo.com/";
}
}
</script>
</head>

<body onload="modifySrc()">
...
<!-- "external script" that generates an
iframe[src="[URL]http://www.google.com/[/URL]"] element;
let it be the only one here for simplicity of
the example -->
<script src="..." type="text/javascript"></script>
...
</body>
[...]

There is a fundamental error in this flow.

There is not. Obviously you have not even tried it.
If the script is "Hard-coded" to the body the result would be like so:

No, it will not be empty if client-side scripting is supported.

And if client-side scripting is not supported, your
document.write('<script ...') would not work anyway.
Lets say an ad is displaying in the "script iframe"
the page loads and writes the "Script Iframe" therefore creating a
"server hit" to the ad publisher, resolving with a cache request (I
mean money) for their service...

The `script' element that triggers the external script creates the
"server hit" whether it is generated dynamically or not.
the correct flow would be to load the correct Iframe from the beginning

To be blunt, you are not qualified to label something as correct regarding
client-side scripting.

Unless you learn to quote eventually, this will be my last response to
one of your postings.


PointedEars
 
S

shlomi.schwartz

Thomas ... don't bother... It's all clear to me after reading this post

http://www.thescripts.com/forum/thread150264.html

Here is a quote for you:

"ralf: Please ignore Thomas, we are trying to get him through puberty
but
until we manage to survive those hectic days in his life, he continues
to ramble incoherently about attribution novels without having a clue
what he is talking about. Until we can manage to either train him,
educate him, or eradicate him, ignore his babbling about attributions."
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 20 Feb
2006 16:45:30 remote, seen in Thomas
'PointedEars' Lahn said:
Furthermore, you should not use consecutive calls of document.write.

No reason here to avoid that. Code should be written to be
comprehensible to its author and maintainers. Any extra time taken by
document.write will here be negligible in comparison with the time taken
in dealing with the http:// references.

However, one should look for ways to reduce repetitiveness in code.
Accumulate the strings to be written instead and write them once, e.g.

var rnd = Math.random(); // still prone to the bug described above, though

var a = [rnd, '<br>'];

if (rnd > 0.5){
a.push('<iframe src="http://www.yahoo.com"><\/iframe>');
}
else
{
a.push('<iframe src="http://www.google.com"><\/iframe>');
}

document.write(a.join(""));

OP : use something like

var rnd = Math.random();
document.write('<iframe src="http://www.' +
(rnd>=0.5?'google':'yahoo') +
'.com"><\/iframe>');

or

document.write('<iframe src="http://www.' +
(Math.random()>=0.5?'google':'yahoo') +
'.com"><\/iframe>');
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Thomas 'PointedEars' Lahn [...] posted :
Furthermore, you should not use consecutive calls of document.write.

No reason here to avoid that. [...]

The reason is that it is error-prone. Examples and more detailed
explanations have been posted before.
Accumulate the strings to be written instead and write them once, e.g.

var rnd = Math.random(); // still prone to the bug described above,
though

var a = [rnd, '<br>'];

if (rnd > 0.5){
a.push('<iframe src="http://www.yahoo.com"><\/iframe>');
}
else
{
a.push('<iframe src="http://www.google.com"><\/iframe>');
}

document.write(a.join(""));

OP : use something like

var rnd = Math.random();
document.write('<iframe src="http://www.' +
(rnd>=0.5?'google':'yahoo') +
'.com"><\/iframe>');

or

document.write('<iframe src="http://www.' +
(Math.random()>=0.5?'google':'yahoo') +
'.com"><\/iframe>');

I am afraid that this probably more efficient, but also more complicated
code does not qualify as a solution to the "cached reload" problem of the
OP either.


PointedEars
 

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,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top