使用Javascript(event.preventDefault();)在弹出窗口中打开链接

使用Javascript(event.preventDefault();)在弹出窗口中打开链接,javascript,html,popup,Javascript,Html,Popup,此问题已在前面得到回答,并在此处结束: 但如果你往下看,你会看到这样的评论: 请注意,我使用的是这种方法,如果您不允许浏览器上出现弹出窗口,效果很好,但是,如果您这样做,它将打开弹出窗口两次。我通过添加event.preventDefault()解决了这个问题;单击链接,并从windowpop()的开头删除返回键Madaah 2013年10月18日18:13 我遇到了两次弹出的相同问题,不知道如何使用event.preventDefault() 函数windowpop(url、宽度、高度){

此问题已在前面得到回答,并在此处结束:

但如果你往下看,你会看到这样的评论:

请注意,我使用的是这种方法,如果您不允许浏览器上出现弹出窗口,效果很好,但是,如果您这样做,它将打开弹出窗口两次。我通过添加event.preventDefault()解决了这个问题;单击链接,并从windowpop()的开头删除返回键Madaah 2013年10月18日18:13

我遇到了两次弹出的相同问题,不知道如何使用event.preventDefault()

函数windowpop(url、宽度、高度){
变量leftPosition,topPosition;
//考虑到边界。
leftPosition=(window.screen.width/2)-(width/2)+10;
//允许标题栏和状态栏。
topPosition=(window.screen.height/2)-(height/2)+50);
//打开窗户。
window.open(url,“Window2”,“status=no,height=“+height+”,width=“+width+”,resizable=yes,left=“+leftPosition+”,top=“+topPosition+”,screenX=“+leftPosition+”,screenY=“+topPosition+”,toolbar=no,menubar=no,滚动条=no,location=no,directories=no”);
}

  • 在函数末尾,添加
    返回false。这将隐式执行
    preventDefault()
    ,因为内联处理程序正在返回函数的返回值。或者,不要使用内联点击处理程序;在windows.open行之后,没有成功!
    function windowpop(url, width, height) {
        var leftPosition, topPosition;
        //Allow for borders.
        leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
        //Allow for title and status bars.
        topPosition = (window.screen.height / 2) - ((height / 2) + 50);
        //Open the window.
        window.open(url, "Window2", "status=no,height=" + height + ",width=" + width + ",resizable=yes,left=" + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY=" + topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");
    }
    
    
    <li>
        <a class="sprite_stumbleupon" href="http://www.stumbleupon.com/submit?url=http://www.your_web_page_url" target="_blank" onclick="return windowpop(this.href, 545, 433)"></a>
    </li>