简体   繁体   English

如何在HTML中调用外部js文件变量

[英]How to call external js file variable in HTML

I have a js file. 我有一个js文件。

File Name: propoties.js 文件名: propoties.js

function simple()
{
    alert("simple");
    var text = "Control";
}

These is my html code. 这些是我的html代码。 What i want is alert that text variable in html. 我想要的是提醒HTML中的文本变量。

<html>
<script type='text/javascript' src='path/propoties.js'></script>    
<script>
    simple();
    alert(text); /* It is not working */
</script>
 </html>

Please help me these. 请帮我这些。 Thank you. 谢谢。

Your js file: 您的js文件:

var simple=function(){
   var textMultiple = {
        text1:"text1",
        text2:"text2"
    };
   return textMultiple;
}

In your html: 在您的html中:

<html>
    <script type='text/javascript' src='./relative/path/to/propoties.js'></script>    
    <script>
      alert(simple().text1);
      alert(simple().text2);
    </script>
 </html>

here is a plunkr demo. 这是一个plunkr演示。

If you want to alert the text in external file you need to declare the variable as global like below. 如果要警告外部文件中的文本,则需要将变量声明为global,如下所示。

var text = "Control";
function simple()
{
    text="Changed";
    alert("simple");   
}

or you can declare the variable using window keyword 或者您可以使用window关键字声明变量

function simple()
{
    alert("simple");   
    window.text = "Control";
}

please check in plunker http://plnkr.co/edit/HjkwlcnkPwJZ7yyo55q6?p=preview 请签入插件http://plnkr.co/edit/HjkwlcnkPwJZ7yyo55q6?p=preview

like you did it the "text" variable is only set in the scope of the function "simple" . 就像您所做的那样,“文本”变量仅在函数“简单”的范围内设置。

you should make the "text" variable global by declaring it outside the function. 您应该通过在函数外部声明“ text”变量来使其成为全局变量。

var text = "";

function simple()
{
    alert("simple");
    text = "Control";
}

Declaring the variable globally will work. 全局声明变量将起作用。 ie

var text = "";
function simple()
{
     text = "Control";
}

see plunker 朋克

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

相关问题 在html中调用外部.js文件 - call external .js file in html 如何使用node.js在html中调用外部javascript文件 - how to call external javascript file in html using node.js 在html中调用外部js文件的功能 - call an external js file's function in html 如何从 html 中的内联 js 脚本获取变量并将其放入外部 .js 文件中? - How do i get a variable from an inline js script in html and put it into an external .js file? 如何将变量值从外部JS jQuery文件传递到html文件中的函数? - How can a pass a variable value from an external JS jQuery file to a function in a html file? 如何通过从html传递变量以调用下面的.js文件来调用document.ready内部的外部.js文件 - how to call external .js which is inside document.ready by passing in variables from html to call the .js file below 如何在JavaScript中将变量传输到外部js文件? - How to transfer a variable to a external js file in javascript? 如何在外部js文件中设置/重新定义变量 - How to set/redefine variable in external js file 在Django环境中,如何使HTML从外部js文件中调用javascript函数 - How to make html call a javascript function from an external js file, in Django environment 如何从外部js文件的onload或onclick事件调用函数到我的html中 - How to call function from external js file onload or onclick event into my html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM