Need a javascript guru (I need help!)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jarrod Hyder

    #1

    Need a javascript guru (I need help!)

    Ok, I wrote my own weblog and I'm working on the web interface for
    adding/editing my posts. I decided to add a little preview button...when
    the button is clicked it is suppose to open a pop-up window. I wrote the
    whole preview function in javascript so that I don't have to reload the
    page to see a preview.

    Here is the code I wrote that doesn't work at all:

    Code:

    function generate_previe w() {
    var newWin =
    window.open('/weblog/pop_up.html','P review','width= 650,height=400' );

    var previewText = document.forms[0].logtext.value; var previewTitle =
    document.forms[0].title.value; var previewIcon = document.previe w.src;

    var insertPoint_Tex t = newWin.document .getElementById ('pre'); var
    content_Text = newWin.document .createTextNode (previewText);
    insertPoint_Tex t.appendChild(c ontent_Text);

    var insertPoint_Tit le = newWin.document .getElementById ('title'); var
    content_Title = newWin.document .createTextNode (previewTitle);
    insertPoint_Tit le.appendChild( content_Title);

    var insertPoint_Ico n = newWin.document .getElementById ('icon');
    insertPoint_Ico n.src = previewIcon;
    }



    And here is the code that works flawlessly to generate the preview:

    Code:

    function generate_previe w() {
    var newWin =
    window.open('/weblog/pop_up.html','P review','width= 650,height=400' );
    newWin2=window. open();
    newWin2.close() ;

    var previewText = document.forms[0].logtext.value; var previewTitle =
    document.forms[0].title.value; var previewIcon = document.previe w.src;

    var insertPoint_Tex t = newWin.document .getElementById ('pre'); var
    content_Text = newWin.document .createTextNode (previewText);
    insertPoint_Tex t.appendChild(c ontent_Text);

    var insertPoint_Tit le = newWin.document .getElementById ('title'); var
    content_Title = newWin.document .createTextNode (previewTitle);
    insertPoint_Tit le.appendChild( content_Title);

    var insertPoint_Ico n = newWin.document .getElementById ('icon');
    insertPoint_Ico n.src = previewIcon;
    }



    It absolutely will not work unless I add these two lines:

    Code:

    newWin2=window. open();
    newWin2.close() ;


    It's really weird and I don't know how to fix the problem without opening
    another window other than the one that I'm generating the preview in. I'm
    not a programmer and I'm not that great with javascript, so if a
    javascript guru could point me in the right direction I would really
    appreciate it.
  • Lee

    #2
    Re: Need a javascript guru (I need help!)

    Jarrod Hyder said:
    [color=blue]
    >It absolutely will not work unless I add these two lines:
    >
    >Code:
    >
    >newWin2=window .open();
    >newWin2.close( );
    >
    >
    >It's really weird and I don't know how to fix the problem without opening
    >another window other than the one that I'm generating the preview in. I'm
    >not a programmer and I'm not that great with javascript, so if a
    >javascript guru could point me in the right direction I would really
    >appreciate it.[/color]

    window.open() returns before the window actually exists.

    Those lines are creating enough of a delay to allow your newWin to
    open and render content and be ready for you to start manipulating
    its document object.

    Since you're opening an HTML page in that window, one option would
    be to put script in that page that loads the content from the
    opener window, so that it pulls new content when it is ready,
    instead of having the opener window start pushing it too soon.

    Comment

    Working...