问题描述:

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?

网友答案:

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

Live Demo

<html>
<head>
</head>
<script>
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
 }
</script>
<body>
<a href="http://www.yahoo.com" onclick="return popitup(this)">yahoo</a>
<a href="http://www.google.com" onclick="return popitup(this)">Google</a>
<a href="http://www.msn.com" onclick="return popitup(this)" target="MSN">MSN</a>
</body>
</html>
网友答案:

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

相关阅读:
Top