MintY
MintY

Reputation: 643

How to open a url in new window without toolbar, menubar

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

Answers (4)

user4717133
user4717133

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

mplungjan
mplungjan

Reputation: 177685

You have the following issues

  1. spaces in parms
  2. most browsers will ignore the status=no and more - scrollbars for example
  3. you follow the link even if you open the window - return false or as I show, false if the popup did the job, true if not
  4. no need for form. Links are not form elements and do not have value but href

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

Sridhar R
Sridhar R

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>

DEMO

Upvotes: 1

Konstantin Dinev
Konstantin Dinev

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

Related Questions