IE javascript window.open problem

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

    IE javascript window.open problem

    Hello,

    I am using the following javascript code to open a new window.
    Somehow, IE always opens a new window. It doesn't open target url in
    the window name given.

    All i want is, if there is a window "MyWin" already open, then the
    main window should load the target url in "MyWin" and not open a new
    window again.

    <!-- Code Starts -->
    <html><head></head>
    <Script Language="javas cript">
    function Callme()
    {
    var win2 = window.open("Te st2.htm", "MyWin");
    }

    /* I don't want to use following type of code as it serves the purpose
    initially but not if you refresh the main page.*/
    /*
    function Callme(location ){
    var win2;
    if (typeof win2 == "object") {
    win2.location.r eplace(location );
    }
    else{
    win2=window.ope n(location,"MyW in",null,true );
    }
    }
    // thanks
    */

    </Script>
    <body>
    <form>
    <input type="button" value="Click Me" onClick="Callme ();">
    </form>
    </body>
    </html>
    <!-- Code Ends -->

    Any hint/help apprreciated.
    IE version : 6.0.2800.1106.x psp2.030422-1633
    OS : Windows XP

    Thanks,
    Samir
  • Grant Wagner

    #2
    Re: IE javascript window.open problem

    Samir Pandey wrote:
    [color=blue]
    > Hello,
    >
    > I am using the following javascript code to open a new window.
    > Somehow, IE always opens a new window. It doesn't open target url in
    > the window name given.
    >
    > All i want is, if there is a window "MyWin" already open, then the
    > main window should load the target url in "MyWin" and not open a new
    > window again.
    >
    > <!-- Code Starts -->
    > <html><head></head>
    > <Script Language="javas cript">[/color]

    <script> tags should appear either inside <head>...</head> or inside
    <body>...</body>.

    The "language" attribute is deprecated. Use type="text/javascript"
    instead.
    [color=blue]
    > function Callme()
    > {
    > var win2 = window.open("Te st2.htm", "MyWin");
    > }
    >
    > /* I don't want to use following type of code as it serves the purpose
    > initially but not if you refresh the main page.*/
    > /*
    > function Callme(location ){
    > var win2;
    > if (typeof win2 == "object") {[/color]

    Because you are declaring win2 on every call to the function, it will
    _never_ be typeof "object". If you want to do something like this, use a
    global variable to track the existance of win2. This task is covered in
    the FAQ:

    <url: http://jibbering.com/faq/#FAQ4_10 />

    --
    | Grant Wagner <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *


    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-deve...upgrade_2.html


    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: IE javascript window.open problem

      Samir Pandey wrote:[color=blue]
      > I am using the following javascript code to open a new window.
      > Somehow, IE always opens a new window. It doesn't open target url in
      > the window name given.
      >
      > All i want is, if there is a window "MyWin" already open, then the
      > main window should load the target url in "MyWin" and not open a new
      > window again.
      >
      > <!-- Code Starts -->
      > <html><head></head>[/color]

      In Valid (X)HTML, the "head" element must not be empty. It must at
      least contain a "title" element. Besides, have you used a proper
      DOCTYPE declaration? If not, that would make the document invalid,
      too. You cannot build a house on a swamp.
      [color=blue]
      > <Script Language="javas cript">[/color]

      <script type="text/javascript">
      [color=blue]
      > function Callme()
      > {
      > var win2 = window.open("Te st2.htm", "MyWin");
      > }
      >
      > /* I don't want to use following type of code as it serves the purpose
      > initially but not if you refresh the main page.*/
      > /*
      > function Callme(location ){
      > var win2;
      > if (typeof win2 == "object") {[/color]

      Since win2 is declared local (note the `var' keyword), this branch will
      be never executed as `win2''s value and type is always `undefined'.
      [color=blue]
      > win2.location.r eplace(location );
      > }
      > else{
      > win2=window.ope n(location,"MyW in",null,true );[/color]

      You maybe run into a scope problem here. `location' is already a
      property of the window object. Use another variable identifier.

      window.open has only three arguments: The URI of the resource to
      access, the internal name of the new Window object, and a *string*
      containing definitions of its features. I wonder what you think
      `null' and `true' will do.

      And there is no need to check if the window is already open if you use
      a unique internal window name. If it is not open, it will be opened,
      if it is still open, it will be reused. All you may want to do is to
      focus the window so it does not remain in the background if it is reused.
      [color=blue]
      > }
      > }[/color]

      function isMethodType(s)
      {
      return (s == "function" || s == "object");
      }

      function callMe(sURI)
      {
      var result = window.open(sUR I, "MyWin", "");

      if (result && isMethodType(ty peof result.focus))
      {
      result.focus();
      }

      return result;
      }

      var win2;
      // ...
      win2 = callMe("a_fool" );
      [color=blue]
      > // thanks[/color]

      ?
      [color=blue]
      > */[/color]

      As you comment everything out, ...
      [color=blue]
      > </Script>[/color]

      </script>
      [color=blue]
      > <body>
      > <form>
      > <input type="button" value="Click Me" onClick="Callme ();">[/color]

      .... why do you expect this to work anyway?
      [color=blue]
      > [...]
      > IE version : 6.0.2800.1106.x psp2.030422-1633
      > OS : Windows XP[/color]

      Ohh, would just anyone post this precise information in the first place!


      PointedEars

      Comment

      Working...