window.open affecting parent window

D

dev1null2

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!
 
L

Lee

(e-mail address removed) said:
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?

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

<a href="#" onclick="createMyWindow()">

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="createMyWindow();return false"
 
R

RobG

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

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.
If you don't want to follow that link, your onclick handler
must return false:

onclick="createMyWindow();return false"

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>
 
D

dev1null2

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top