Reputation: 643
here i am trying to open a URL in new window using window.open
I have a list of anchor tags with some URL, When i click on those anchor tags the javascript should get the href="" value and pass the value to the javascript function.
Here is code what i have done:
<html>
<head>
</head>
<script>
var a;
function popitup(a)
{
window.open(a,
'open_window',
'menubar=no, toolbar=no, location=no, directories=no, status=no, scrollbars=no, resizable=no, dependent, width=800, height=620, left=0, top=0')
}
</script>
<body>
<form name="popup" >
<a href="http://www.yahoo.com" onclick="popitup(this.value)">yahoo</a>
<a href="http://www.google.com" onclick="popitup(this.value)">Google</a>
<a href="http://www.msn.com" onclick="popitup(this.value)">MSN</a>
</form>
</body>
</html>
When i click the yahoo, the www.yahoo.com should get opened in new window.. Similarly all
but now when i click on those links i am getting the error "server not found" in the new window.
How can i solve this?
Upvotes: 4
Views: 27728
Reputation:
lastest update with ES6 Javascript based on mplungjan Solution
document.addEventListener("click", navigateTo, false);
function navigateTo(event){
if (event.target.matches('a')) {
window.open(event.target.href,"_blank",'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,dependent,width=800,height=620,left=0,top=0');
}
event.preventDefault();
return false;
}
<html>
<head>
</head>
<body>
you need run this locale to check Code Snippet not allow Popups<br>
<a href="http://www.yahoo.com">yahoo</a>
<a href="http://www.google.com">Google</a>
<a href="http://www.msn.com">MSN</a>
</body>
</html>
Upvotes: 1
Reputation: 177685
You have the following issues
IF you do not have a popup blocker AND you do not have "open new windows in tabs" THEN this code may work in your browser. If there is a popup blocker, the link still works
function popitup(link) {
var w = window.open(link.href,
link.target || "_blank",
'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,dependent,width=800,height=620,left=0,top=0');
return w ? false : true; // allow the link to work if popup is blocked
}
<a href="https://www.yahoo.com" onclick="return popitup(this)">yahoo</a>
<a href="https://www.google.com" onclick="return popitup(this)">Google</a>
<a href="https://www.msn.com" onclick="return popitup(this)" target="MSN">MSN</a>
These parms are what I would use
'resizable,width=800,height=620,left=0,top=0'
Upvotes: 11
Reputation: 20408
<html>
<head>
</head>
<script>
var a;
function popitup(a)
{
window.open(a,
'open_window',
'menubar=no, toolbar=no, location=no, directories=no, status=no, scrollbars=no, resizable=no, dependent, width=800, height=620, left=0, top=0')
}
</script>
<body>
<form name="popup" >
<a href="http://www.yahoo.com" onclick="popitup(this.href)">yahoo</a>
<a href="http://www.google.com" onclick="popitup(this.href))">Google</a>
<a href="http://www.msn.com" onclick="popitup(this.href))">MSN</a>
</form>
</body>
</html>
Upvotes: 1
Reputation: 34895
You this.href
instead of this.value
in your onclick
handlers. The href
attribute of an anchor tag is not its value.
Upvotes: 2