473,839 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic IFRAME problem

using this example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Test Page</title>

</head>

<body>

<SCRIPT LANGUAGE="JavaS cript" TYPE="text/javascript">
var rnd = Math.ceil(Math. random()*10);
document.write( rnd);
if(rnd > 5){
document.write( '<br><iframe src="http://www.yahoo.com"> </iframe>');
}else{
document.write( '<br><iframe src="http://www.google.com" ></iframe>');
}
</SCRIPT>

</body>
</html>

This simple code writes an IFRAME element with a random src attribute
(Google , yahoo).
The code works fine over IE, but have a strange behavior on Fire Fox
1.5

It seems that the first src chosen is the dominate one and would not
replace it self when reloading the page (F5).

Any workaround?

Feb 20 '06 #1
26 3284
oops ... It seems that IE is doing the same scenario.

help?

Feb 20 '06 #2
here is an IE Workaround

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test Page</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaS cript" TYPE="text/javascript">
var rnd = Math.ceil(Math. random()*10);
document.write( rnd);
var f = document.create Element("iframe ");
f.id = "frm"
document.body.a ppendChild(f)
if(rnd > 5){
document.getEle mentById("frm") .src =
"http://www.google.com" ;
}else{
document.getEle mentById("frm") .src = "http://www.yahoo.com";
}
</SCRIPT>
</body>
</html>

still dose not work on firefox

HELP

Feb 20 '06 #3
sh************* @gmail.com wrote:
using this example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
You should include the system identifier (URL of the DTD), Quirks Mode is
triggered otherwise:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
[...]
<SCRIPT LANGUAGE="JavaS cript" TYPE="text/javascript">
You can safely omit the `language' attribute, then declare HTML 4.01 Strict.
var rnd = Math.ceil(Math. random()*10);
document.write( rnd);
if(rnd > 5){
document.write( '<br><iframe src="http://www.yahoo.com"> </iframe>'); ^^ }else{
document.write( '<br><iframe src="http://www.google.com" ></iframe>'); ^^
ETAGO delimiters must be escaped in HTML `script' elements.
}
</SCRIPT>

</body>
</html>

This simple code writes an IFRAME element with a random src attribute
(Google , yahoo). The code works fine over IE, but have a strange
behavior on Fire Fox 1.5

It seems that the first src chosen is the dominate one
While I have been writing this, now I have read both your original posting
and your two followups. And the question occurred to me:

Do you really understand (the math of) random numbers?

The first branch should not be dominant. `rnd' can assume one of the
integer values 1 to 10 (because Math.random() returns IEEE-754 doubles x
from x = 0 to x < 1, and Math.ceil() returns the ceiling of its argument as
a IEEE-754 double). So the first condition is true iff `rnd' assumes one
of the values 6 to 10, that are 5 out of 10. Therefore, the statistical
probability of that event is 0.5 (or 50% or 1:2) as is the statistical
probability of the (opposite) event that `rnd' assumes one of the values 1
to 5.

However, probability is what can be observed only with a _large_ number of
repetitions of a random experiment. (This is the [Weak] Law of Large
Numbers that states, loosely speaking, that the relative frequency of an
event converges in a limit called the probability of that event as the
repetitions of the corresponding random experiment approach [positive]
infinity.)

So it is entirely possible (even though `rnd' is a computed random number
which differ from real random numbers), although not probable, that with
nine or nine hundred thousand repetitions of the experiment under the same
start conditions (in your case no-cache reloads, see below) the first
branch of the code is used each time, without violating this law.

The only reason why the probability of X=6..10 (X being the outcome of the
random experiment) could be greater than the probability of X=1..5 here is
that Math.random() could return 1 (which it should not according to
ECMAScript, but earlier Opera implementations are known for that). In that
case (X={1, 2, ..., 9, 10, ceil(1*10)}), the event X > 5 ({6, 7, 8, 9, 10,
10}) would occur with a probability of 6:11 and the opposite event (1 <= X
<= 5) would have to occur with a probability of only 5:11. However,
JavaScript (as implemented in Firefox) is not known to have this particular
bug in its Math.random() implementation.

That explained, you can simplify your little random experiment a lot:

var rnd = Math.random(); // still prone to the bug described above, though
// ...
if (rnd > 0.5)
{
// ...
}
else
{
// ...
}

Furthermore, you should not use consecutive calls of document.write.
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(""));
and would not replace it self when reloading the page (F5).

Any workaround?


Ctrl+F5 or Ctrl+Shift+R, that overrides the browser cache.
PointedEars
Feb 20 '06 #4
hi Thomas, thanks for the fast reply.

All your remarks were contributing.
unfortunately the problem is more complex.

the main issue is that:

when writing the IFRAME dynamically (use example 1) the first "src" is
somehow cached in the browser DOM.

allow me to explain a scenario:

1) Lets say the page loads and the variable rnd is set to 1
2) The documet.write() is called with Google src IFRAME
3) All is good the Google frame is shown
4) F5 ... refreshing
6) the page loads and the variable rnd is set to 9
7) The documet.write() is called with Yahoo src IFRAME
8) ERROR the Google frame is displayed

furthermore if we write "javascript:ale rt(document.bod y.innerHTML)" in
the address bar we can see that the IFRAME src is set correctly and
still the browser displays the wrong frame.

Any New idea?
;-)

Feb 20 '06 #5
sh************* @gmail.com wrote:
[...]
when writing the IFRAME dynamically (use example 1) the first "src" is
somehow cached in the browser DOM.
As I said. And it is not cached in the browser DOM, but in
the browser cache :)
allow me to explain a scenario:

1) Lets say the page loads and the variable rnd is set to 1
2) The documet.write() is called with Google src IFRAME
3) All is good the Google frame is shown
4) F5 ... refreshing
As I said, if you refresh with F5 you use the browser cache;
with Ctrl+F5 or Ctrl+Shift+R you do not.
6) the page loads and the variable rnd is set to 9
7) The documet.write() is called with Yahoo src IFRAME
8) ERROR the Google frame is displayed
I observed the same behavior in

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.1) Gecko/20060209
Debian/1.5.dfsg+1.5.0. 1-2 Firefox/1.5.0.1

only that I also observed that when the first time the yahoo.com homepage
was displayed in the iframe, refresh with the F5 key resulted in displaying
yahoo.com even though rnd <= 5 (or <= 0.5; should have displayed the
google.com homepage).

It does not matter if the markup is Valid or not (escaping ETAGOs), if the
`language' attribute is used or not, if the document is rendered in Quirks
Mode or Standards Compliance Mode, if Math.ceil(Math. random() * 10) or
Math.random() is used, if document.write( ) is called one time or more
(although all of those /are/ valid concerns), if document.write( ) is used
or document.append Child(). With Ctrl+R/F5 the previously generated
`iframe' element is used no matter the computed value of `rnd'.[1]

With overriding the browser cache as I described above and before,
this is not the case here. I am pretty sure it is the same with IE.

I have observed the same behavior in Gecko-based browsers (particularly
Mozilla/5.0 Navigator and Firefox) with the value of form controls before,
for example; with cached refresh (Ctrl+R or F5) they retain the changed
value, without using the cache (Ctrl+Shift+R or Ctrl+F5), they show the
initial value. Scripts are refreshed always nevertheless (this may be
due to default use of Cache-Control on my servers, though).
furthermore if we write "javascript:ale rt(document.bod y.innerHTML)" in
the address bar we can see that the IFRAME src is set correctly and
still the browser displays the wrong frame.
Apparently the `innerHTML' property partly does not use the browser cache.
Fascinating.
Any New idea?
;-)


No, the old one is still the solution to the problem. At least here.
HTH

PointedEars

P.S.: <URL:http://www.safalra.com/special/googlegroupsrep ly/>
___________
[1] And presumably just to show me that I understood statistical probability
properly, the Universe decided to give me a value greater than 0.5 eight
times in a row while was testing this <g/>
Feb 20 '06 #6
> when writing the IFRAME dynamically (use example 1) the first "src" is
somehow cached in the browser DOM.


You can avoid caching by appending an everchanging time to the URL of
the src.

Specifically:

document.getEle mentById("frm") .src =
"http://www.google.com? ts' + (new Date()).getTime ()

Ann

Feb 20 '06 #7
Giggle Girl wrote:
when writing the IFRAME dynamically (use example 1) the first "src" is
somehow cached in the browser DOM.


You can avoid caching by appending an everchanging time to the URL of
the src.

Specifically:

document.getEle mentById("frm") .src =
"http://www.google.com? ts' + (new Date()).getTime ()


What you are proposing means to access an `iframe' element that was created
and appended/included _before_ which is not the same thing as the OP tried.
With your approach, it would not even be necessary to "append an
everchanging time" (which would be a Bad Thing anyway).

Although I still do not understand why there is a problem at all (a cached
refresh is a /cached/ refresh) this approach (modifying the `iframe'
element afterwards) would lead to a solution. I would prefer to use the
`frames' collection then, though.
PointedEars
Feb 20 '06 #8
On Mon, 20 Feb 2006 20:39:48 +0200, Giggle Girl <mi*******@gmai l.com>
wrote:
when writing the IFRAME dynamically (use example 1) the first "src" is
somehow cached in the browser DOM.


You can avoid caching by appending an everchanging time to the URL of
the src.

Specifically:

document.getEle mentById("frm") .src =
"http://www.google.com? ts' + (new Date()).getTime ()


Almost the same idea - the use of own cgi script, which then calls other
sites:

src="http://....some_script .cgi?site=http://www.google.com. .."
Vladas
Feb 20 '06 #9
Vladas Saulis wrote:
[...] Giggle Girl [...] wrote:
when writing the IFRAME dynamically (use example 1) the first "src" is
somehow cached in the browser DOM.

You can avoid caching by appending an everchanging time to the URL of
the src.

Specifically:

document.getEle mentById("frm") .src =
"http://www.google.com? ts' + (new Date()).getTime ()


Almost the same idea - the use of own cgi script, which then calls other
sites:

src="http://....some_script .cgi?site=http://www.google.com. .."


"Appending an everchanging time" was unnecessary already, now this is
complete overkill.
PointedEars
Feb 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9371
by: Csaba2000 | last post by:
I want to be able to embed a single quote into an INPUT element within a dynamically generated IFRAME. The example below shows how the IFRAME is generated. Challenge: I'd like the alert box to show: Mom said, "Don't" instead of the current: Mom said, "Do not". The point of the exercise is that I will be passing arbitrary strings to the IFRAME and I want to ensure that the final string is the same as the original. So I'm interested...
2
47355
by: Templar | last post by:
Hi i'm bothering with such problem... I must dynamic create an Iframe, and then put som raw HTML into it. But I can't. When I create iframe, I can't access its properties. Here's the coe snipplet: //************************************* //*************************************
1
1242
by: Tammy | last post by:
Hi I am working on a portal that will serve up reporting services reports. The user will have a basic home page and from there will be able to run their reports. As each report is run, I need to be able to add a tab to the form that will link them to the report (which I have within another form in an Iframe) until they decide they want to close the report, at which point I will remove the tab. I was planning on using one webform with an...
1
4308
by: tribal boy | last post by:
Guys, I am using a dynamic menu which uses xml,xsl a css file and javascript. This works fine when there are no server controls around or underneath it. The problem is whenever the menu encounters a server control, it closes itself.
8
5375
by: hyejin | last post by:
I have a problem with dynamic iframe and document.close() on Firefox. Below two files create a dynamic iframe by JavaScript. These two samples do not have any problems on IE. But, on Firefox, the icon on the top corner keeps running with "loading" message on the bottom status bar even though the browser completed everything in the iFrame. The line that causes the problem is "document.close()" in the included JS file. If this line is...
1
5384
by: mike888 | last post by:
I want to create dynamic iframe content like below but in Firefox <iframe width="100%" height="100" src="#"></iframe> <script language="JavaScript"><!-- document.frames.document.open(); document.frames.document.write('THis is the dynamic text for iframe above'); document.frames.document.close();
1
6895
by: redbaks | last post by:
Hi! I am trying to implement adding widgets to our template editor (for blogs). I am worried that i might open a window for XSS attacks so i decided to enveloped all widgets inside an iframe. whenever a user wants to insert an external widget to his blog, a script takes over and it will create a dynamic iframe and then the widget script will be inserted inside the iframe. i created a dynamic iframe using this code (I simplified it..)
3
5401
polymorphic
by: polymorphic | last post by:
I have succeeded in embedding PDF files in a dynamic iframe. The problem is that I need the PDF to cache. If the PDF remains the same from page load to page load then the pdf is somehow cached with the html page. But if I try to navigate to another pdf in the IFRAME then no caching occurs. Is the problem in the IFRAME reloading instead of just refreshing the pdf? <SCRIPT type="text/javascript"> var pageNo; var nav; var iframe; var...
1
4926
by: cdmsenthil | last post by:
I have an Infragistics UltrawebGrid . Each Row in the grid is attached to a context menu using Infragistics CSOM Upon click on the menu, I am creating an Iframe dynamically which points to another page in the same domain which also contains infragistics datagid populated with default data retrieved from Data Base. After creating the frame I am attaching it to the HTML DOM and show it as modal popup with OK and Cancel Button inside an...
0
9697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10908
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10648
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10293
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7828
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7017
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4484
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3134
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.