<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
 
<html> 
<head> 
	<title>JavaScript Window Open Example</title> 

</head> 
 
<body> 
<SCRIPT language="JavaScript1.2"> 
function openwindow()
{

	location('http://www.google.com','_self');
}
</SCRIPT> 
<P> 

 <input type="button" value="Replace Me!" onClick=" window.location = 'http://www.google.com/' ; ">

<A href="javascript: openwindow()" >LINK 1</A> 
</P> 
<P>Back to the article:<br> 
<a href="http://google.lk" target="_self">LINK 2</a> 
</P> 
</body> 
</html>

In this There are 2 links, in the 1st link, when we click on it, the web page will open in a new window. and when we right click on the link the (open link in new tab/window) popup doesn't appear.

In the 2nd link, when i click on it, it will open up in the same window, and also when i right click on it the popup that shows the (open link in new tab/window) popup appears.

So what i want to do is to edit the Javascript in the 1st link to have both functions;
1.) when a user click on the link, the web page should open in the same window
2.) when a user R- Click on the link, the popup should show up (open link in new tab/window).

Can some one please help me here, i'm lost :(

Dani AI

Generated

The root cause in ’s example is that the first link isn’t a normal, navigable anchor (its href is a JavaScript call). Many browsers only show “Open link in new tab/window” when an <a> has a real URL in its href; links that use javascript: or have no meaningful href often lose that context-menu option. was right to be cautious, and ’s mouse-button detection shows one way to branch on clicks — but a simpler, more robust approach is to give the anchor a proper href and only intercept plain left-clicks if any extra JS is needed.

A minimal, modern pattern that preserves the context menu and keyboard/middle-click behavior:

<a id="myLink" href="https://example.com">LINK 1</a>

<script>
document.getElementById('myLink').addEventListener('click', function (e) {
  // let Ctrl/Cmd, middle-click and other modifier actions behave normally
  if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;

  // 'click' is the primary (left) button in most browsers;
  // optional custom logic for left-click goes here.
  // To do custom navigation, prevent default then set location:
  // e.preventDefault();
  // window.location.href = this.href;
});
</script>

Practical notes and troubleshooting:

  • Do not use href="javascript:..." or no-href anchors when a real URL exists; give the <a> the actual URL.
  • Avoid calling preventDefault() except when handling left-click navigation explicitly; preventing default on other events can remove the context-menu options.
  • If supporting very old IE, special button-value checks may be needed; otherwise the click handler pattern is sufficient.
  • Opening a new window programmatically on right-click is discouraged (and can be blocked by popup blockers) — preserve the browser’s native right-click behavior so “Open in new tab/window” remains available.

This keeps left-click navigation in the same tab while preserving the right-click “open in new tab/window” menu and common modifier shortcuts.

Recommended Answers

All 2 Replies

I dont think this will be possible. Whenever you right click on a link and try to open it in new tab/window it will open with the value set to the 'href' attribute

Try attaching this function to your link. Replace url with your url and name with whatever name you want.

function(e){
var leftBtn;
if(!e){
var e = window.event;
leftBtn = 1; //IE detects left click differently
}else{
leftBtn = 0;
}
if(e.button===2){ //Right click
window.open(url, name)
}else if(e.button===leftBtn){ //Left click
window.location = url;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.