Two ways of opening a new window

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

    Two ways of opening a new window

    What's the difference between the two ways of opening a new window and
    writing to it?

    Let's say I prepare a code that goes into a new window and save it in a
    variable winHTML:

    var winHTML = "<HTML><HEAD><T ITLE>Any title</TITLE><BODY>Any
    text</BODY></HEAD>";

    Now, I have two ways to open a new window and write that code into the new
    window:

    One way is:
    var newWin=window.o pen();
    newWin.document .open();
    newWin.document .write(winHTML) ;
    newWin.document .close();
    newWin.focus();

    Other way is:
    window.open("ja vascript:opener .winHTML");

    Is there any reason why to prefer one over the other?


  • Andrew Thompson

    #2
    Re: Two ways of opening a new window

    On Wed, 26 May 2004 17:04:03 GMT, Amir wrote:
    [color=blue]
    > var winHTML = "<HTML><HEAD><T ITLE>Any title</TITLE><BODY>Any
    > text</BODY></HEAD>";[/color]

    ...perhaps you should validate that as HTML first.

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology

    Comment

    • Amir

      #3
      Re: Two ways of opening a new window

      Andrew Thompson wrote:[color=blue]
      >[color=green]
      > > var winHTML = "<HTML><HEAD><T ITLE>Any title</TITLE><BODY>Any
      > > text</BODY></HEAD>";[/color]
      >
      > ..perhaps you should validate that as HTML first.
      >
      > --
      > Andrew Thompson
      > http://www.PhySci.org/ Open-source software suite
      > http://www.PhySci.org/codes/ Web & IT Help
      > http://www.1point1C.org/ Science & Technology[/color]

      var winHTML = "<HTML><HEAD><T ITLE>Any title</TITLE><BODY>Any
      text</BODY></HTML>";

      However, the JavaScript question was as clear as it is now. I wouldn't
      attach too much importance to minor HTML errors, unless I present a question
      that requires running a code.


      Comment

      • Lee

        #4
        Re: Two ways of opening a new window

        Amir said:[color=blue]
        >
        >What's the difference between the two ways of opening a new window and
        >writing to it?
        >
        >Let's say I prepare a code that goes into a new window and save it in a
        >variable winHTML:
        >
        >var winHTML = "<HTML><HEAD><T ITLE>Any title</TITLE><BODY>Any
        >text</BODY></HEAD>";
        >
        >Now, I have two ways to open a new window and write that code into the new
        >window:
        >
        >One way is:
        > var newWin=window.o pen();
        > newWin.document .open();
        > newWin.document .write(winHTML) ;
        > newWin.document .close();
        > newWin.focus();
        >
        >Other way is:
        > window.open("ja vascript:opener .winHTML");
        >
        >Is there any reason why to prefer one over the other?[/color]

        I routinely use the second. The window.open() method returns
        without guaranteeing that the window has been created yet, so
        there have been many posts to this newsgroup asking why that
        method fails some of the time.

        I've never had any trouble with the second form.
        That sort of thing is exactly what the "javascript :"
        pseudo-protocol was created for.

        note that you can send focus to the window (as you do in your
        first example), with:

        window.open("ja vascript:opener .winHTML").focu s();

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Two ways of opening a new window

          Amir wrote:
          [color=blue]
          > Andrew Thompson wrote:[color=green][color=darkred]
          >>> var winHTML = "<HTML><HEAD><T ITLE>Any title</TITLE><BODY>Any
          >>> text</BODY></HEAD>";[/color]
          >>
          >> ..perhaps you should validate that as HTML first.[/color]
          > [...]
          >
          > var winHTML = "<HTML><HEAD><T ITLE>Any title</TITLE><BODY>Any
          > text</BODY></HTML>";[/color]

          Nothing has changed, this is still invalid HTML. Besides, the ETAGO
          delimiters (</) can cause trouble as it marks the end of the element in
          SGML. If you use that sequence in scripts embedded in HTML documents,
          escape it. In J(ava)Script you would do that with "<\/".
          [color=blue]
          > However, the JavaScript question was as clear as it is now. I wouldn't
          > attach too much importance to minor HTML errors, unless I present a
          > question that requires running a code.[/color]

          You can't build a house on a swamp.

          And please, trim your quotes and use a "From" header
          that complies with Internet/Usenet standards.


          PointedEars

          Comment

          Working...