将动态值传递给eventListener

时间:2016-07-20 08:53:40

标签: javascript jquery

我想知道为什么我这样做

window.addEventListener('unload', function() {

ajaxLeaveRoom(rid, uid, urlwr);

});

它有效,但当我这样做时

var evsup = "unload"; 

window.addEventListener(evsup, function() {

ajaxLeaveRoom(rid, uid, urlwr);

});

它不起作用!有没有一种特殊的方法将该var传递给Listener?

我需要动态var,因为我正在测试unload或者在另一个函数中的beforeload,所以我做了一个静态var来测试但它不起作用......

谢谢

这是完整的代码

{literal}
<script type="text/javascript">

/* yes the literals are backwords thats how it works */
/* for smarty in js - see smarty forum */
/* addEventListener is ok we do not support < ie9 */
/* do not use function(event) it does not work on ie */

"use strict";

var r = '?ran='+Math.floor((Math.random() * 10000) +1);
var ajaxurl = {/literal} '{$ajaxUrl}'{literal};
var rid  = {/literal} '{$room}' {literal};
var uid  = {/literal} '{$userid}' {literal};
var urlwr = ajaxurl+r; 


$( document ).ready(function() {

//var evsup = checkEventSupport();

//alert(sup);

var evsup = "unload"; /* or beforeunload */

alert(evsup);

});


window.addEventListener(evsup, function() {

ajaxLeaveRoom(rid, uid, urlwr);

});

</script>
{/literal}

0 个答案:

没有答案