Results 1 to 7 of 7

Thread: Write a dynamic iframe

  1. #1
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default Write a dynamic iframe

    Hi,

    I wonder if the following is coded correctly:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html> 
    <head> 
    <title>Sample Page</title>
    </head> 
    <body> 
    <div id="container"></div>
    <script type="text/javascript">
    var url = "http://www.example.com/";
    var content = "<iframe width='800' height='600' frameborder='0' src='" + url + "'><\/iframe>";
    document.getElementById("container").innerHTML = content;
    </script>
    </body>
    </html>
    Many thanks in advance!
    Mike

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    It should be:
    Code:
    <body> 
    <div id="container"></div>
    <script type="text/javascript">
    var url = "http://www.example.com/";
    var content = "<iframe width='800' height='600' frameborder='0' src=" + url + "><\/iframe>";
    document.getElementById("container").innerHTML = content;
    </script>
    </body>
    Apart from that, this is not dynamic coding. It's the same as writing the iframe directly to the page.
    If you want to dynamically write the iframe to the page, you would do something like:
    Code:
    <head> 
    <script type="text/javascript">
    function dynamic_iframe(id,content)
    {
    document.getElementById(id).innerHTML = "<iframe width='800' height='600' frameborder='0' src=" + content +"><\/iframe>";
    }
    </script>
    </head> 
    <body> 
    <a href="javascript: void(0)" onclick="dynamic_iframe('container', 'http://www.example.com')">load</a>
    <div id="container"></div>
    </body>
    Of course, the onclick could be replaced with an onload.
    ===
    Arie Molendijk.

  3. #3
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by molendijk View Post
    It should be:.....
    Thanks for the answer and correction! But there's something I don't understand: I put the variable in single quotation marks -- just like the other attributes values. What's wrong with it?

    Of course, the onclick could be replaced with an onload.
    Is that what you mean:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html> 
    <head> 
    <title>Sample Page</title>
    <script type="text/javascript">
    function dynamic_iframe(id,content)
    {
    document.getElementById(id).innerHTML = "<iframe width='800' height='600' frameborder='0' src=" + content + "><\/iframe>";
    }
    </script>
    </head> 
    <body onload="dynamic_iframe('container', 'http://www.example.com');"> 
    <div id="container"></div>
    </body>
    </html>

  4. #4
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default

    One more question regarding using plus signs, please:

    Code:
    "<iframe height='600' frameborder='0' src=" + content + " width=" + value + "><\/iframe>"
    Is the above correct if there are two iframe variables?

  5. #5
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Quote Originally Posted by Rain Lover View Post
    Thanks for the answer and correction! But there's something I don't understand: I put the variable in single quotation marks -- just like the other attributes values. What's wrong with it?
    The other values are (fixed) constants, so they are put between appropriate quotation marks. But the url is a variable, and therefor isn't put between quotation marks.
    To be more precise:
    - we have two main 'constant-strings':
    <iframe width='800' height='600' frameborder='0' src= and ><\/iframe> which should both be put between quotation marks. We used " " for it here.
    - within the first string, we have 'sub-constants' for width, height and frameborder. Being constants, they must put between quotation marks too. As we have already used "" for the first main constant, we have to use other marks for the sub-constants: ' '.

    Quote Originally Posted by Rain Lover View Post
    Is that what you mean:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html> 
    <head> 
    <title>Sample Page</title>
    <script type="text/javascript">
    function dynamic_iframe(id,content)
    {
    document.getElementById(id).innerHTML = "<iframe width='800' height='600' frameborder='0' src=" + content + "><\/iframe>";
    }
    </script>
    </head> 
    <body onload="dynamic_iframe('container', 'http://www.example.com');"> 
    <div id="container"></div>
    </body>
    </html>
    Yes, that's what I mean.
    ===
    Arie.
    Last edited by molendijk; 11-12-2011 at 06:12 PM. Reason: Correction

  6. #6
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Quote Originally Posted by Rain Lover View Post
    One more question regarding using plus signs, please:
    Code:
    "<iframe height='600' frameborder='0' src=" + content + " width=" + value + "><\/iframe>"
    Is the above correct if there are two iframe variables?
    Yes, that's perfect. So now you can do something like:
    Code:
    <head> 
    <title>Sample Page</title>
    <script type="text/javascript">
    function dynamic_iframe(id,content,value)
    {
    document.getElementById(id).innerHTML = "<iframe height='600' frameborder='0' src=" + content + " width=" + value + "><\/iframe>";
    }
    </script>
    </head> 
    <body onload="dynamic_iframe('container', 'http://www.example.com', '900px');"> 
    <div id="container"></div>
    </body>
    Arie.

  7. The Following User Says Thank You to molendijk For This Useful Post:

    Rain Lover (11-12-2011)

  8. #7
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default

    Very informative! Thank you!

Bookmarks

Posting Permissions

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