Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shift+Mouse Left click opens the parent window and the clicked link 1

Status
Not open for further replies.

ajindal

Programmer
Oct 7, 2003
11
IN
When I try to open a link I use SHIFT + LEFT MOUSE I know that it opens the link in a new window. Originally , I am using window.open which also opens the link in a new window even if I don't press shift. However for some strange reason this not only opens the link in a new window, But also opens the parent window again. I don't know why.
Can anyone tell a reason and sol to this problem.
 

because you're using shift+click, it will open the JS "window.open" in a new window, which will then execute and open the new window.

You cannot control this, AFAIK. Just don't press shift+click with window.open links ;o)

Dan
 
Hi, I am posting the sample code that I am trying to execute and the eror is coming. i have tried it on IE 6.0 version.
The code for a file caller.html is given below.
U'll have to make another empty html file "test.html" which will be called on clicking the link in caller.html.
Try clicking on the link with the shift button presses. U'll find that 2 windows are opened. I don't want to disable the shift button.If I don't use the alert and let the script mentioned below return "false", still two windows get opened as u will c. If I use alert, the onclick event does not get fired.
I have tried using the code:
<script>
function mouseDown(e)
{
var shiftPressed=0;
shiftPressed=event.shiftKey;
if (shiftPressed) {
//alert ('Shift-click is disabled.')
return false;
}
return true;
}
document.onmousedown = mouseDown;
</script>



----------------------------------
Caller.html starts here-->
-----------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Calling Window</TITLE>
</HEAD>
<BODY>
<A href="#" onclick="openWindow();">Click Here to Open Another Window</a>
</BODY>
</HTML>
<script language = javascript>
function openWindow()
{
window.open("test.html","","");
}
</script>


------------------------------------
test.html
------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Called </TITLE>
</HEAD>
<BODY>
</BODY>
</HTML>


 
Add "return false" to disable the default behavior of the link:

Code:
<A href="#" onclick="openWindow();return false">

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Actually, to correctly handle right-clicking too, you should write it like this:
Code:
<HTML>
<HEAD>
<TITLE> Calling Window</TITLE>
<script language = javascript>
function openWindow(url)
{
    window.open(url,"","");
}
</script>
</HEAD>
<BODY>
<A href="test.html" onclick="openWindow(this.href);return false">Click Here to Open Another Window</a>
</BODY>
</HTML>


Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Why not take it one step further! In case they have JavaScript disabled:
Code:
<HTML>
<HEAD>
<TITLE> Calling Window</TITLE>
<script language = javascript>
function openWindow(url)
{
    window.open(url,"","");
}
</script>
</HEAD>
<BODY>
<A href="test.html" onclick="openWindow(this.href);return false" [b]target="_blank"[/b]>Click Here to Open Another Window</a>
</BODY>
</HTML>

Of course if you're not changing any features of the window, like disabling the toolbar, you don't even need JavaScript:
Code:
<a href="test.html" target="_blank">

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Thanx a lot Adam.... It is working...If you have time, can you tell me why do we need to return false?

Anyways...thanx a billion
 
Sure,
In your original link, you had the url "#". When you click the link, the browser tries to find a bookmark on the current page with a name that matches the text after the #. Since there is no text after the #, the browser will either ignore it or move to the top of the page.

When you add window.open() to your link, you are adding additional functionality to the link. In other words, the link still tries to find the bookmark. So when you use Shift+Click, the browser does what it would normally do (open the window and try to find the bookmark) plus it executes the window.open().

So in you're link, you use window.open to open a new window, then you add "return false" to the onclick event to tell the browser to prevent what would normally happen on a click event.

You can add "return false" to any event that allows it to prevent the default behavior from occurring.

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Thanx a lot Adam....Can you suggest me some site where I can learn the basics
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top