打开弹出窗口会返回错误

时间:2013-10-31 14:12:33

标签: javascript

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;
}

我在umbraco页面中通过以下html代码调用上面的javascript。

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

我一直收到javascript错误,说新窗口未定义。困惑为什么它一直在发生,因为我已经明确定义了它!请帮忙

1 个答案:

答案 0 :(得分:1)

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

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:您可以重用此函数,因为它从此获取href(具有id action-request-tickets的元素)