rich text editing

Discussion in 'Javascript' started by mel, Aug 23, 2007.

  1. mel

    mel Guest

    hi all =)

    why does this works ...

    <html>
    <body>

    <iframe id="my_frame" src="blank.html">
    </iframe>

    <script>

    document.getElementById("my_frame").contentDocument.designMode = "On";
    </script>

    </body>
    </html>


    ... and this doesn't ?

    <html>
    <body>

    <iframe id="my_frame">
    </iframe>

    <script>
    document.getElementById("my_frame").src =
    "blank.html";

    document.getElementById("my_frame").contentDocument.designMode = "On";
    </script>

    </body>
    </html>


    PS in case you are wondering, it is important for me to change
    dinamically the src field of the iframe ... btw, is this possible ?
     
    mel, Aug 23, 2007
    #1
    1. Advertising

  2. mel

    elephant Guest

    > why does this works ...

    I'm not positive why the second doesn't work, but, I think it's
    because design mode needs to be run after the area loads

    > PS in case you are wondering, it is important for me to change
    > dinamically the src field of the iframe ... btw, is this possible ?


    Yep, here you go

    <script type='text/javascript'>
    function changeSrc(newUrl) {
    document.getElementById('myframe').setAttribute('src',
    newUrl);
    }
    function designModeOn(sentIframe) {
    sentIframe.contentDocument.designMode = "On";
    }
    </script>
    <body>
    <iframe id='myframe' src='deleteMe.html'
    onload='designModeOn(this)'></iframe>
    <button onclick='changeSrc("test.html");' />
    </body>

    Customize to fit your needs,
     
    elephant, Aug 23, 2007
    #2
    1. Advertising

  3. mel

    mel Guest

    great !! it works thanks !!!! I think I was doing almost the same, i
    think the difference is that i was using iframe.src = "newlocation"
    and you are using the setAttribute method =)

    thanks !!!

    BTW, is it possible to have an empty iframe (withou any src property)
    and change its contents with innerHTML or something ?


    On Aug 23, 4:21 pm, elephant <> wrote:
    > > why does this works ...

    >
    > I'm not positive why the second doesn't work, but, I think it's
    > because design mode needs to be run after the area loads
    >
    > > PS in case you are wondering, it is important for me to change
    > > dinamically the src field of the iframe ... btw, is this possible ?

    >
    > Yep, here you go
    >
    > <script type='text/javascript'>
    > function changeSrc(newUrl) {
    > document.getElementById('myframe').setAttribute('src',
    > newUrl);
    > }
    > function designModeOn(sentIframe) {
    > sentIframe.contentDocument.designMode = "On";
    > }
    > </script>
    > <body>
    > <iframe id='myframe' src='deleteMe.html'
    > onload='designModeOn(this)'></iframe>
    > <button onclick='changeSrc("test.html");' />
    > </body>
    >
    > Customize to fit your needs,
     
    mel, Aug 24, 2007
    #3
  4. mel

    elephant Guest

    > BTW, is it possible to have an empty iframe (withou any src property)
    > and change its contents with innerHTML or something ?


    I'm not sure I understand the question, is there a reason for using an
    empty iframe, as opposed to an empty div or some other tag? Or a
    reason not to have a src tag?

    I haven't tried using this with an iframe without a src, but, with a
    src attribute pointing to about:blank it works.

    You can use the frames object, so say you have this iframe code ->

    <iframe src='blank.html' name='template'></iframe>

    you can then edit it with this javascript

    newDocument = window.frames['template'].document;
    newDiv = newDocument.createElement('div');
    newDiv.setAttribute('id','myNewDiv');
    newDocument.body.appendChild(newDiv);
    newDocument.getElementById('myNewDiv').innerHTML = "blah blah blah";

    Although a note, while innerHTML has very good browser support, and in
    most cases is faster than creating and appending new nodes, it is not
    a W3C standard. Which means, it is subject to have different
    implementations in different browsers, not all browsers make the new
    elements part of the DOM, and there's no guarantee that it will
    continually appear in browsers. If this is something you hope to last
    a while, do this

    myTextNode = newDocument.createTextNode('blah blah blah');
    newDiv.appendChild(myTextNode);

    you can also throw new elements in there.

    Not a huge deal, but, I think it's a good habit to get into.

    If I've misunderstood, or something doesn't make sense, or doesn't
    work, let me know.
     
    elephant, Aug 24, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    457
  2. Erik
    Replies:
    3
    Views:
    576
  3. Martin Honnen

    Rich Text Editing

    Martin Honnen, Jan 2, 2005, in forum: Javascript
    Replies:
    1
    Views:
    99
    Börni
    Jan 2, 2005
  4. Replies:
    7
    Views:
    301
  5. deostroll
    Replies:
    1
    Views:
    163
    deostroll
    Jan 11, 2008
Loading...

Share This Page