打开弹出窗口返回错误

Opening a pop-up window returns error

本文关键字:返回 错误 窗口      更新时间:2023-09-26
function RequestTickets(url) {
var settings = 'height=500,width=400,location=no,resizable=no,scrollbars=no,status=no,
    titlebar=yes,toolbar=no';
var newwindow = window.open(url, '-- Ticket Request Form --', settings, false);
if (window.focus) { newwindow.focus() };
return false;
}

我有上面的javascript被umbraco页面中的以下一段html代码调用。

<a href="javascript:RequestTickets('/us/home/requestform.aspx')">here</a> to request tickets for an event.

我不断收到一个javascript错误,说newwindow未定义。困惑为什么它不断发生,因为我已经明确定义了它!请帮忙

通常,

我通过addEventListener(在大多数情况下使用jQuery事件)而不是html在javascript中创建html元素和javascript函数之间的绑定。

document.getElementById('action-request-tickets').addEventListener('click', function(event){
    event.preventDefault();
    var settings = 'height=500,width=400,location=no,resizable=no,scrollbars=no,status=no,titlebar=yes,toolbar=no';
    var newwindow = window.open(this.href, '-- Ticket Request Form --', settings, false);
    if (window.focus) { 
        newwindow.focus() 
    };
    return false;
});

http://jsfiddle.net/DvHnP/

Benifit:你可以重用这个函数,因为它从这个(带有id的元素action-request-tickets)获取href