简体   繁体   English

JS函数调用在Chrome和Opera(JS + jQuery)中不起作用

[英]JS function call won't work in Chrome and Opera (JS + jQuery)

I can't seem to get the following functionality to work under Chrome and Opera (latest versions on Windows XP). 我似乎无法在Chrome和Opera (Windows XP的最新版本)下使用以下功能。 The code is: 代码是:

$.getScript('/js/modules/'+module+'.js', function() {
    setTimeout('window.'+module+'_init()', 800);
});

Everything seems to work fine, the script loads, init function exists (a few debug alerts within that setTimeout statement verified that type of "window.module_init" really is a function ) but the function just won't run. 一切似乎工作正常,脚本已加载,init函数存在(该setTimeout语句中的一些调试警报已验证“ window.module_init”的类型确实是一个函数 ),但该函数将无法运行。

I tried putting a simple alert at the beginning of that init function, leave ONLY an alert there - nothing helped. 我尝试在该init函数的开头放置一个简单的警报,仅在其中留一个警报-没有任何帮助。

I must say I'm quite puzzled by this, as this works just fine under Firefox and MSIE. 我必须说,对此我感到很困惑,因为在Firefox和MSIE下它可以正常工作。

FYI, the init function in that external js file simply looks like this: 仅供参考,该外部js文件中的init函数如下所示:

function notifications_init() {
    alert('test');
}

" notifications " is the value of my "module" variable above notifications ”是我上面的“ module”变量的值

Any advice is greatly appreciated :-) 任何意见是极大的赞赏 :-)

setTimeout(window[module+'_init'], 800);

How about setTimeout('window.'+module+'_init', 800); setTimeout('window.'+module+'_init', 800); , without the braces? ,没有大括号?

I've just tested what you are explaining here and it worked great here. 我刚刚测试了您在这里解释的内容,在这里效果很好。 I am guessing that the external js file that you are loading using $.getScript contains errors. 我猜您正在使用$.getScript加载的外部js文件包含错误。

Could you comment everything inside that file except for the notifications_init() you have in your question? 除了问题中的notifications_init()外,您能否注释该文件中的所有内容?

This is what I did: 这是我所做的:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        alert('start');
        var module = 'test';
        $.getScript('/Scripts/' + module + '.js', function () {
            alert('script was loaded and executed');
            setTimeout('window.' + module + '_init()', 800);
        });
        alert('finish');
    });
</script>

And test.js contains only this: 而test.js仅包含以下内容:

function test_init() {
    alert('test function');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM