更好的方法来编写这个弹出功能?

时间:2011-07-29 00:54:47

标签: javascript

function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 
       'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=400,height=600,left=600,top=300');");
}

<a href="javascript:popUp('http://google.com')">Open the Popup Window</a>

这就是我现在的代码。我想更改它,以便它定位像“popup”这样的特定类,并且与该类的所有链接都会生成一个弹出窗口,其中包含href中的链接。

有点像这样:

<a class="popup" href="http://google.com">Open the Popup Window</a>

我将如何实现这一目标?

3 个答案:

答案 0 :(得分:1)

 function popUp(URL){
day = new Date();
    id = day.getTime();
      window.open(URL,'"+id+"','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=400,height=600,left=600,top=300');
 }

答案 1 :(得分:1)

最佳优化是删除它,让访问者决定是否要在新窗口中打开链接。至少,将代码移动到监听器:

<a href="http://google.com" onclick="return popUp(this.href);">Open the Popup Window</a>

然后在弹出功能中,如果函数正确执行,则返回false。

答案 2 :(得分:0)

很抱歉对派对迟到了,但这是我的2美分:

使用jquery来做你的例子

<a class="popup" href="http://google.com">Open the Popup Window</a>

你可以使用:

var page = [], id=0;
$('.popup').click(function(e){
    e.preventDefault();
 // add to array of open popups
page.push(window.open(this.href,"page"+id,'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=400,height=600,left=600,top=300')); 
     id++;
    return false;
});

然后你会使用这样的东西来关闭弹出窗口。

$('button').click(function(e){
   page.reverse();
   page.pop().close(); 
   page.reverse();
});
相关问题