Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    running javascript within iframe

    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.
    )


    Code:
    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>
    Last edited by amitye; 12-10-2009 at 11:46 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,300
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    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.

  • #4
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    375
    Thanks
    9
    Thanked 55 Times in 54 Posts
    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?

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by amitye View Post
    I am trying to run javascript within Iframe and its not running.

    Code:
    <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:
    Code:
    <iframe>Your browser does not support iframes</iframe>
    Quote Originally Posted by hdewantara
    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)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •