-2

Example:

<script type="text/javascript">
    function test() {
        console.log("Hello world");
    }
</script>

How would I call test()?

Edit: I didn't explain this correctly.

I am using the request module of node.js to load an external html file that contains javascript functions:

request.get(options, function (error, response, body) {
    if (error && response.statusCode !== 200) {
    }
    else {
        jsdom.env({
            html: body,
            scripts: ["../jquery-1.6.4.min.js"]
        }, function (err, window) {
            var $ = window.jQuery;

I'm just looking for the syntax to call a function in 'body'.

Esailija
  • 138,174
  • 23
  • 272
  • 326
camden_kid
  • 12,591
  • 11
  • 52
  • 88

4 Answers4

5

So the problem here is that by default, jsdom.env does not execute javascript found while processing markup.

You'll need to turn these features on:

jsdom.env({
  // ...
  features : {
    FetchExternalResources : ['script'],
    ProcessExternalResources : ['script']
  }
});

FetchExternalResources controls whether or not jsdom should even bother reaching across the network/disk to collect the bytes of a resource

ProcessExternalResources controls whether or fetched scripts are executed

Note these names were chosen to encompass other resources types (read: images, css, etc..) which will be added in the future. The idea here is to provide sane defaults, but have many turnable knobs that affect the behavior of jsdom.

tmpvar
  • 1,331
  • 1
  • 8
  • 12
3

Just call it like any other function on your page, jQuery is a framework and is not needed for running a JS function.

Jakub
  • 20,418
  • 8
  • 65
  • 92
1

hmmm... probably I'd go with

test();

But that's not jquery, it's plain old javascript.

Sirs
  • 1,277
  • 17
  • 30
1

Try this:

 ...

     function (err, window) {
                    var $ = window.jQuery;
                    window.test();

...

You could also try:

<script type="text/javascript" id="myscript">
    function test() {
        console.log("Hello world");
    }
</script>

And then:

function (err, window) {
                        var $ = window.jQuery;
                        (1,window.eval)( $("#myscript").html() );
                        window.test();
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • When I try that I get I the following error message: TypeError: Object # has no method 'test' – camden_kid Dec 02 '11 at 09:27
  • @camden_kid, that's where the function should be defined if it's properly done :/. Like the jQuery you passed as a script is also found there, so are all of the global functions...Are you sure scripts are parsed like this in the html? https://github.com/tmpvar/jsdom – Esailija Dec 02 '11 at 16:07
  • Esailija - When I write 'body' to a file I can see all the functions defined in scripts. I'm not sure what you mean by 'parsed'. I know I'm getting the correct html data. I just can't work out how to call one of the functions. I used this [http://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object](http://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object) to see what window contained and could not see the function (although I'm grasping at straws here and I'm not sure if the function was meant to be shown). – camden_kid Dec 02 '11 at 16:21
  • @camden_kid, all functions that are declared like you show in the OP can be found in the global object (window). I.E.: `function h(){} window.h();` If the test function is not found in the global object and you can't do `window.test()` then there is an error in jsdom in how it parses script tags' function. I cannot really find any information on whether what you are trying to do is possible at all, in fact. – Esailija Dec 02 '11 at 16:33
  • Esailija - I think you're right. I now understand what you mean by 'parsed' and I don't think it is, as I should be able to access the functions as you say. Frustrating. BTW I tried your last suggestion and that does not work. Thanks for your help though. – camden_kid Dec 02 '11 at 16:53
  • @camden_kid, you can try debugging it... does `$("#myscript").html()` do anything? log it and so on... – Esailija Dec 02 '11 at 16:56
  • Esailija - console.log($("#myscript").html()) writes out the function definition. – camden_kid Dec 02 '11 at 20:44