window.open affecting parent window

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dev1null2@hotmail.com

    #1

    window.open affecting parent window

    Hi!

    I'm using window.open to create a secondary window, and everything is
    working fine with that. My problem is that as soon as that window is
    opened, the parent window scrolls to the top of its page. So when the
    user closes the secondary window, they've lost their place in the
    parent document.

    Is this normal behavior for window.open? Is there a quick way to
    prevent it, or is it more likely a problem with my Javascript?

    thanks!



  • Lee

    #2
    Re: window.open affecting parent window

    dev1null2@hotma il.com said:[color=blue]
    >
    >Hi!
    >
    >I'm using window.open to create a secondary window, and everything is
    >working fine with that. My problem is that as soon as that window is
    >opened, the parent window scrolls to the top of its page. So when the
    >user closes the secondary window, they've lost their place in the
    >parent document.
    >
    >Is this normal behavior for window.open? Is there a quick way to
    >prevent it, or is it more likely a problem with my Javascript?[/color]

    A problem with your Javascript.
    You must be creating the window using something like:

    <a href="#" onclick="create MyWindow()">

    The # is not a comment character. It's not a place holder.
    It is a link to the top of the current page.

    If you don't want to follow that link, your onclick handler must return false:

    onclick="create MyWindow();retu rn false"

    Comment

    • RobG

      #3
      Re: window.open affecting parent window

      Lee wrote:
      [...][color=blue]
      > The # is not a comment character. It's not a place holder.
      > It is a link to the top of the current page.[/color]

      I don't think that is strictly correct. The HTML spec says
      that an anchor specified with "#" must match an ID or NAME
      attribute of an element, but that the anchor may be left
      empty and completed with script later.

      <URL:http://www.w3.org/TR/html4/struct/links.html#edef-A>

      Therefore href="#" is valid HTML but the spec does not say
      what a browser should do with it. However, most browsers
      seem to interpret it as being the current document and
      scroll to the top.
      [color=blue]
      >
      > If you don't want to follow that link, your onclick handler
      > must return false:
      >
      > onclick="create MyWindow();retu rn false"[/color]

      The anchor (href) should point to the URL that would be
      opened in the new window by JavaScript if it were enabled.
      If JavaScript is diabled, the user will be taken to the anchor
      and not be left with a link that (maybe) just scrolls to the
      top of the page:

      <a href="http://www.google.com" onclick="
      createMyWindow( 'http://www.google.com' );
      return false;">Go to Google</a>

      --
      Rob

      Comment

      • Randy Webb

        #4
        Re: window.open affecting parent window

        RobG wrote:[color=blue]
        > Lee wrote:
        > [...]
        >[/color]

        <--snip-->
        [color=blue]
        > <a href="http://www.google.com" onclick="
        > createMyWindow( 'http://www.google.com' );
        > return false;">Go to Google</a>[/color]

        <a href="http://www.google.com" target="myWindo w"
        onclick="create MyWindow(this.h ref,this.target );
        return false">Go to Google in a new window</a>

        --
        Randy
        comp.lang.javas cript FAQ - http://jibbering.com/faq

        Comment

        • dev1null2@hotmail.com

          #5
          Re: window.open affecting parent window



          You are quite right that I was using onClick to trigger a new window,
          where the onClick was linked to a button in a form. The 'return
          false' addition did the trick. Thanks very much! :>D



          On 24 Jan 2005 18:18:39 -0800, Lee <REM0VElbspamtr ap@cox.net> wrote:
          [color=blue]
          >dev1null2@hotm ail.com said:[color=green]
          >>
          >>Hi!
          >>
          >>I'm using window.open to create a secondary window, and everything is
          >>working fine with that. My problem is that as soon as that window is
          >>opened, the parent window scrolls to the top of its page. So when the
          >>user closes the secondary window, they've lost their place in the
          >>parent document.
          >>
          >>Is this normal behavior for window.open? Is there a quick way to
          >>prevent it, or is it more likely a problem with my Javascript?[/color]
          >
          >A problem with your Javascript.
          >You must be creating the window using something like:
          >
          ><a href="#" onclick="create MyWindow()">
          >
          >The # is not a comment character. It's not a place holder.
          >It is a link to the top of the current page.
          >
          >If you don't want to follow that link, your onclick handler must return false:
          >
          > onclick="create MyWindow();retu rn false"[/color]

          Comment

          Working...