0

i execute 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?

TorbenL
  • 1,219
  • 1
  • 11
  • 15

2 Answers2

1

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.

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
1

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 ); 
tim
  • 31
  • 2
  • That is quite useful and interesting to know. – Andrew Weir Oct 08 '10 at 13:29
  • A variable beginning with `$` is nothing special, it works just like any other letter. For reference, see this question: http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign – Henrik Karlsson Jun 16 '12 at 16:09