[Javascript] Iframe: load same URL using JS

Rees, Mark Mark.Rees at astrazeneca.com
Thu Feb 26 07:49:49 CST 2009


Sorry, I should have mentioned that the original code doesn't work in IE6 or FF3 on Win2K.

Your suggestion doesn't work either - in fact it throws an error in Firefox on the line when you try to set the src like so:

uncaught exception: [Exception... "Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsIDOMHTMLIFrameElement.src]" nsresult: "0x8000ffff (NS_ERROR_UNEXPECTED)" location: "JS frame :: file:///C:/mark/test.htm :: addIframe :: line 25" data: no]

What I have noticed is that setting the src to the current URL as a string works in IE:

iFrame.src='http://this.url';

but not in Firefox

Not a big problem as I can always use another page instead of the same one. I'm just a bit surprised that this isn't possible although I can quite see why it shouldn't be.


--------------------------------------------------------------------------
AstraZeneca UK Limited is a company incorporated in England and Wales with registered number: 03674842 and a registered office at 15 Stanhope Gate, London W1K 1LN.
Confidentiality Notice: This message is private and may contain confidential, proprietary and legally privileged information. If you have received this message in error, please notify us and remove it from your system and note that you must not copy, distribute or take any action in reliance on it. Any unauthorised use or disclosure of the contents of this message is not permitted and may be unlawful.
Disclaimer: Email messages may be subject to delays, interception, non-delivery and unauthorised alterations. Therefore, information expressed in this message is not given or endorsed by AstraZeneca UK Limited unless otherwise notified by an authorised representative independent of this message. No contractual relationship is created by this message by any person unless specifically indicated by agreement in writing other than email.
Monitoring: AstraZeneca UK Limited may monitor email traffic data and content for the purposes of the prevention and detection of crime, ensuring the security of our computer systems and checking Compliance with our Code of Conduct and Policies.
-----Original Message-----
From: javascript-bounces at lists.evolt.org
[mailto:javascript-bounces at lists.evolt.org]On Behalf Of Peter-Paul Koch
Sent: 25 February 2009 21:13
To: JavaScript List
Subject: Re: [Javascript] Iframe: load same URL using JS


You didn't specify the browser.

Nonetheless, at a total guess, try appending the iframe before setting
the src. It just might work.

2009/2/25 Rees, Mark <Mark.Rees at astrazeneca.com>:
> Hello
>
> This doesn't work, and it doesn't work in HTML either. A single iframe is created OK but it is empty.
>
> I suppose there is some security feature in browsers to stop infinite page loads? The thing is I only wish to do it once (as the code below shows). Just curious as to what is stopping it working.
>
> function addIframe(){
>        //reloads the same page in an iframe
>        if(!document.getElementById('iframe')){
>                var iFrame=document.createElement('iframe');
>                iFrame.width=100;
>                iFrame.height=100;
>                //for some reason you cannot reload the same page in the parent
>
>                iFrame.src=location.href;
>
>                iFrame.setAttribute('id','iframe');
>                document.body.appendChild(iFrame);
>        }
> }
>
> function checkIframe(){
>        //checks whether there is already an iframe
>        if(!window.frameElement){
>                alert('i am the parent page');
>                addIframe();
>
>
>
>        }else{
>                //this never runs
>                alert('i am the child page');
>
>        }
> }
>
> window.onload=checkIframe;
>
> Mark



More information about the Javascript mailing list