如何调用脚本标记中定义的javascript函数? [英] How to call a javascript function defined in a script tag?

查看:63
本文介绍了如何调用脚本标记中定义的javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

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

如何调用 test()

编辑:我没有正确解释。

I didn't explain this correctly.

我正在使用node.js的请求模块加载包含javascript函数的外部html文件:

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;

我只是在寻找在'body'中调用函数的语法。

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

推荐答案

所以这里的问题是默认情况下, jsdom.env 在处理标记时不会执行找到的javascript。

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

您需要启用以下功能:

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

FetchExternalResources 控制jsdom是否应该均匀打扰到达网络/磁盘以收集资源的字节

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

ProcessExternalResources 控制是否执行脚本或获取脚本

ProcessExternalResources controls whether or fetched scripts are executed

注意这些名称被选择为包含将来添加的其他资源类型(读取:图像,css等等) 。这里的想法是提供理智的默认值,但有许多可转动的旋钮会影响jsdom的行为。

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.

这篇关于如何调用脚本标记中定义的javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆