我将如何传递数据载入$ .getScript()外部脚本? [英] How would I pass data to an external script loaded with $.getScript()?

查看:683
本文介绍了我将如何传递数据载入$ .getScript()外部脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图加载远程使用jQuery的$ .getScript一个javascript,但我很困惑,我怎么可以将数据传递到外部脚本。

So I'm trying to load a javascript remotely using jquery's $.getScript, but I'm puzzled on how I can pass data to the external script.

我已经尝试设置呼叫前变量,但他们不是在被加载脚本可用的,当我尝试发送/使用的查询字符串检索它们,远程脚本试图读取该基地的查询字符串文件,它会从,本身不叫。是否有任何其他方式做到这一点?或者是有可能有一个JavaScript文件中读取自己的查询字符串,而不是它的距离(这是在浏览器中加载)?

I've tried setting variables before the call but they aren't available in the script that gets loaded, and when I try to send/retrieve them using the query string, the remote script tries to read the querystring of the base file that it gets called from, not itself. Is there any other way to do this? Or is it possible to have a javascript file read its own querystring rather than the file it's called from (that's loaded in the browser)?

// editor ini
var editor_ini = { page: current_page, action: 'edit' };
var foo = 'bar';
// load the editor
$.getScript('assets/desktop/desklets/'+launcher.config.editor+'/execute.js', function(){});

在execute.js文件,在 editor_ini 都是不可用的,我得到了同样的结果

In the execute.js file, the editor_ini and foo are both unavailable, I get the same result with:

// load the editor
$.getScript('assets/desktop/desklets/'+launcher.config.editor+'/execute.js', { page: current_page, action: 'edit', foo: 'bar' }, function(){});

由于远程脚本似乎得到从原始文档,而不是调用文件时使用一个查询字符串。

because the remote script seems to be getting the query string from the original document rather than the one used when calling the file.

如果它的事项,我试图使用查询对象插件的jQuery的读取查询字符串

If it matters, I was trying to use the query object plugin for jquery for reading the query string.

推荐答案

在内嵌的JavaScript声明的全局变量是外部JavaScript页​​面使用加载访问 $。getScript加入脚本()。确保变量是全球

global variable declared in inline javascript is accessible in external javascript page loaded using $.getScript(). Make sure the variable is global

这篇关于我将如何传递数据载入$ .getScript()外部脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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