最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to open a url in new window without toolbar, menubar - Stack Overflow

programmeradmin1浏览0评论

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="" onclick="popitup(this.value)">yahoo</a>
<a href="" onclick="popitup(this.value)">Google</a>
<a href="" onclick="popitup(this.value)">MSN</a>
</form>
</body>
</html>

When i click the yahoo, the www.yahoo 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?

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?

Share Improve this question asked Nov 22, 2013 at 12:21 MintYMintY 6435 gold badges12 silver badges23 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 11

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'

You this.href instead of this.value in your onclick handlers. The href attribute of an anchor tag is not its value.

<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

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>

发布评论

评论列表(0)

  1. 暂无评论