【问题标题】:Fetch a variable from external javascript to execute inside a script block in body从外部 javascript 中获取变量以在正文中的脚本块内执行
【发布时间】:2014-06-30 05:09:27
【问题描述】:

我有一个外部 javascript 文件,例如 first.js,其中包含一个名为“score”的变量。我想在 body 的 <script> 标签内使用这个变量(只有在变量在 first.js 内完成更新之后,换句话说,我还想控制这个 <script> inside body 何时触发,显然,使用“得分”)。有什么办法吗? 编辑 - - - - 基本上我想将此变量存储到 parse.com 的数据浏览器中。这是我的脚本--

    <script>

function giveit(){

var temp=whatScore();



Parse.initialize("myID", "myJDID");


var GameScore = Parse.Object.extend("GameScore");
var gameScore = new GameScore();

gameScore.set("score", temp);

gameScore.save(null, {
  success: function(gameScore) {
    // Execute any logic that should take place after the object is saved.
    alert('New object created with objectId: ' + gameScore.id);
  },
  error: function(gameScore, error) {
    // Execute any logic that should take place if the save fails.
    // error is a Parse.Error with an error code and description.
    alert('Failed to create new object, with error code: ' + error.message);
  }
});



}

</script>

giveit() 函数在单击按钮时被调用。我使用了 onlogin 属性。 现在whatScore()first.js 中定义的函数,如下所示:

function whatScore()
{
return score;
}

我将结果存储在 temp 中,并在下面的代码中使用了 temp,但它不起作用。 在控制台中,它说“whatScore”没有定义。我该怎么做才能使first.js 的whatScore 函数实际上定义在&lt;script&gt; 标记内? 谢谢。

【问题讨论】:

  • 我的伙伴编辑了代码。请看剧本。也可以建议将“分数”变量存储到 parse.com 的任何其他方式。谢谢。
  • @ejay_francisco,这是 parse.com 脚本
  • 嗨,我之前更新了我的代码我的答案。请查看更改后的代码。

标签: javascript jquery html parse-platform


【解决方案1】:

使用此代码:

<!DOCTYPE html>
<html>
    <head>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
        <title>Test</title>
        <script src="first.js" type="text/javascript"></script>
    </head>
    <body><script type="text/javascript">console . log ( { foo : score } );</script></body>
</html>

和文件名为:“first.js”内容:

var score = 5;

我进入控制台:

Object { foo: 5 }

确保:

  1. 您的 HTML5/CSS3/JavaScipt 语法有效。
  2. 变量 score 在全局范围内。
  3. 名为“first.js”的文件在您的脚本标签之前加载,变量 score 在您使用之前已声明和设置。

您也可以使用此代码等待变量:

<!DOCTYPE html>
<html>
    <head>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
        <title>Test</title>
    </head>
    <body>
        <script type="text/javascript">
            function logScore () { console . log ( { foo : score } ); }
            var interval = setInterval ( function ()
            {
                if ( typeof score !== "undefined" ) // if is declared use variable named: "score" and exit interval
                {
                    logScore ();
                    clearInterval ( interval );
                }
            }, 1000 ); // wait one second ...
        </script>
        <script type="text/javascript">var score = 5; /* score loaded after interval */ </script>
    </body>
</html>

更新:

<!DOCTYPE html>
<html>
    <head>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
        <title>Test</title>
    </head>
    <body>
        <button onclick="giveit ();">Test your function here ...</button>
        <script type="text/javascript">
            function giveit ()
            {
                var interval = setInterval ( function ()
                {
                    if ( typeof whatScore === "function" ) // now check if function is declared, use return variable named: "score" and exit interval
                    {
                        var temp = whatScore ();
                        console . log ( { foo : temp } ); // log: Object { foo: 5 }
                        /* your code here */
                        clearInterval ( interval );
                    }
                }, 1000 ); // wait one second ...
            }
        </script>
        <script type="text/javascript">
            var score = 5; /* set global variable named: "score" - loaded after interval */
            function whatScore ()
            {
                // var score = 5 /* or set local variable named: "score" - loaded after interval */
                return score;
            }
        </script>
    </body>
</html>

【讨论】:

  • 您好,请查看我所做的编辑。我非常接近。请帮我。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多