...

View Full Version : running javascript within iframe



amitye
12-10-2009, 10:39 AM
I am trying to run javascript within Iframe and its not running.
(The bigger picture:
The final target is to do a file upload without refreshing the page, and the original code is using the iframe as the target of the form, and the return from the server is a javascript code that should update some status field on the page.
However the code return from the server is not running, so I just paste it in the iframe and it is still not running.
)




Content-type: text/html\n\n
<html>

<script type="text/javascript">
alert("this is running")
</script>

<iframe id="upload_target" name="upload_target" src="#" style="width:100;height:100;border:0px solid #fff;">
<script type="text/javascript">
alert("this not running")
</script>
</iframe>

</html>

Philip M
12-10-2009, 11:27 AM
For security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.

gusblake
12-10-2009, 12:30 PM
Is that your actual code, or have you just done that to show that the script is in the iframe's src page?

I'm pretty sure browsers ignore anything in iframe tags.

hdewantara
12-15-2009, 07:33 AM
It would be helpful to know the HTML script of your form,
the PHP/ASP script which process the form request,
and what has your browser told you about any errors.

If a javascript is going to be executed from its independent iframe/frame,
I guess it should work fine, but it's not a common practice though.

Haven't practice such thing yet so I guess:
a) Your main document which holds the iframe, should be of HTML 4.01 Frameset DTD type.
b) Are you sure the javascript is not pasted from another javascript?

Kor
12-15-2009, 08:59 AM
I am trying to run javascript within Iframe and its not running.



<iframe id="upload_target" name="upload_target" src="#" style="width:100;height:100;border:0px solid #fff;">
<script type="text/javascript">
alert("this not running")
</script>
</iframe>

You script is not within the iframe loaded document. Anything placed between <iframe> and </iframe> will work only if the browser does not support iframes, which is quite rare these days:


<iframe>Your browser does not support iframes</iframe>



Your main document which holds the iframe, should be of HTML 4.01 Frameset DTD type

No. It is not necessary. the DTD might be HTML 4.01 transitional or strict, XHTML transitional or strict. In fact all the modern browsers support frames/iframes even in quirks mode (that means even without a DTD)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum