简体   繁体   English

从jQuery $(document).ready(function(){})中的外部JavaScript文件获取变量值;

[英]Getting variable value from external JavaScript file in jQuery $(document).ready(function(){});

I have defined on external JavaSsript file which contains one variable such as the following 我在外部JavaSsript文件中定义了该文件,其中包含一个变量,如下所示

var demo='Hello'". 

I want to use this variable's value in jQuery's $(document).ready() event. 我想在jQuery的$(document).ready()事件中使用此变量的值。 How can I accomplish this? 我该怎么做?

All you have to do is put the external file above your main script: 您所要做的就是将外部文件放在主脚本上方:

<script src="external.js"></script>
<script>
     // "demo" is accessible here
</script>

Script tags load synchronously, so once the second script tag executes, you can be sure the first one has already loaded. 脚本标签是同步加载的,因此一旦执行了第二个脚本标签,就可以确保第一个已被加载。

Edit 编辑

Per the comment, this script works for me: 根据评论,此脚本适用于我:

<html>
    <script src="global.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.js"></script> 
    <script type="text/javascript"> 
        $(document).ready(function() { var x=abc(); alert('hello'+x); }); 
    </script>
</html>

Where global.js is just: 其中global.js只是:

function abc() { var valu='hello'; return valu; }

If you have defined that file before the file with the ready event, it should be available providing the scope is correct. 如果您在具有ready事件的文件之前定义了该文件,则在范围正确的情况下它应该可用。 ie: 即:

<script type="text/javascript" src="scriptwithvalue.js"></script>
<script type="text/javascript" src="scriptthatwantsthevalue.js"></script>

When I'm passing variables between files, I tend to wrap the logic up in a getter and setter, ie: 当我在文件之间传递变量时,我倾向于将逻辑封装在getter和setter中,即:

File One 文件一

var name = "Dave";

function getName()
{
    return name;
}

File Two 文件二

var name = getName();

Edit 编辑

As above, if your Javascript is already in the HTML file, then simply importing the script with the value in it is enough. 如上所述,如果您的Javascript已经在HTML文件中,则只需导入其中包含值的脚本就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从jQuery(文档)调用外部函数。准备好了吗? - Calling external function from jquery (document).ready? 在jQuery(document).ready()之后从外部位置运行javascript - Run javascript from external location after jQuery(document).ready() 尝试从 jquery $(document).ready 调用外部 js 中定义的 function - Try to call function defined in external js from jquery $(document).ready jQuery使用$ {document)加载外部JavaScript.ready失败 - jQuery load external JavaScript with $(document).ready fails 在 $(document).ready() 中包含外部 jQuery 文件 - Enclosing external jQuery file in $(document).ready() $(document).ready(function(){} jquery被调用两次了吗? - $(document).ready(function(){} jquery is getting called twice? 将全局变量传递给jQuery中的$(document).ready函数 - Pass a global variable to the $(document).ready function in jQuery 使用不同的变量值多次在外部javascript文件中执行$(document).ready()中的代码 - execute code in $(document).ready() in external javascript file multiple times with different variable values 如何将变量值从 $(document).ready(function() 传递到 $(document).on('change','select',function() - How to pass variable value from $(document).ready(function() to $(document).on('change','select',function() jQuery:从外部Javascript文件读取变量 - jQuery: reading variable from external Javascript file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM