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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2009
    Posts
    200
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Keep writing elements into an iframe even after its redirection to different URLs

    I know how to write into an iframe:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>Writer</title>
        <style>
            textarea,
            iframe {
                display: block;
                width: 800px;
                height: 200px;
            }
        </style>
    </head>
    
    <body>
        <textarea id="ta" oninput="writeIt();"></textarea>
        <iframe id="frm"></iframe>
        <script>
            function writeIt() {
                var ta = document.getElementById('ta');
                var frm = document.getElementById('frm');
                var frmDoc = frm.contentDocument;
                frmDoc.open();
                frmDoc.write(ta.value);
                frmDoc.close();
            }
        </script>
    </body>
    
    </html>
    DEMO

    There's a problem, though. Enter the following into the textarea:

    <a href="http://www.example.com/">Example.com</a>

    Now click on the link, and then get back to the textarea to continue writing. It doesn't work anymore due to the cross-origin restriction. Is there any way in this case to continue writing?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,175
    Thanks
    203
    Thanked 2,550 Times in 2,528 Posts
    You can't use JavaScript to get content from other sites. JavaScript can only get content from the same site as the page containing the script. You should block the entry of URLs into your textarea.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 03-15-2014 at 07:48 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,413
    Thanks
    11
    Thanked 595 Times in 575 Posts
    you can set the .src of the iframe back to a local page to continue writing.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #4
    Regular Coder
    Join Date
    Nov 2009
    Posts
    200
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    you can set the .src of the iframe back to a local page to continue writing.
    Is that what you mean:demo. It gives an error in console and doesn't start writing immediately.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,413
    Thanks
    11
    Thanked 595 Times in 575 Posts
    it seemed to work for me, but i would nav to a local blank page, i call one blank.html, can wait for it's onload to fire before writing to ensure you don't write to a booting frame.

    more like Writer - JSFiddle, but without the slow-loading error page that causes blinkiness.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #6
    Regular Coder
    Join Date
    Nov 2009
    Posts
    200
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    it seemed to work for me, but i would nav to a local blank page, i call one blank.html, can wait for it's onload to fire before writing to ensure you don't write to a booting frame.

    more like Writer - JSFiddle, but without the slow-loading error page that causes blinkiness.
    It seems to work, but I still get a security error in console: Editor
    Besides, IMHO it doesn't make sense to clear the frame on each input event by about:blank. It should only happen when the frame src is not about:blank.
    Last edited by Rain Lover; 03-17-2014 at 08:14 PM.

  • #7
    Regular Coder
    Join Date
    Nov 2009
    Posts
    200
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    it seemed to work for me, but i would nav to a local blank page, i call one blank.html, can wait for it's onload to fire before writing to ensure you don't write to a booting frame.

    more like Writer - JSFiddle, but without the slow-loading error page that causes blinkiness.
    I wonder what you think of this method: Writer - JSFiddle
    And here's something similar: http://jsfiddle.net/Mori/VY7uw/6/
    Last edited by Rain Lover; 03-17-2014 at 08:48 PM.


  •  

    Posting Permissions

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