开发者

jQuery $.getScript - old functions & variables in executed script

开发者 https://www.devze.com 2023-01-03 10:37 出处:网络
i ex开发者_如何学JAVAecute a javascript with jQuery $.getScript. In the executed script i haven\'t access to the functions and variables of my source file.

i ex开发者_如何学JAVAecute a javascript with jQuery $.getScript. In the executed script i haven't access to the functions and variables of my source file.

Is there a solution?


The script executed by $.getScript() does have access to the global context. You can use any global variable (or function for that matter) from within your external script.


Nick Craver, I just spend 3 (!) hours obsessing over why my thing wouldn't work, and you gave me the insight I needed to make it work.

XOXOXOXOXOXOXOXO

interesting to note:

you can declare a variable as a jquery var like this:

$variableName = something;

That way jquery also has access to it from anywhere in the scope.

$(function(){ 
    $alertString = 'Hello World'; 

    $.getScript('test.js', function(){ 
        // do nothing 
    });    
} 

test.js: 

alert( $alertString ); 
0

精彩评论

暂无评论...
验证码 换一张
取 消