简体   繁体   English

从外部.js文件动态访问更改的变量

[英]Dynamically accessing changing variable from external .js file

I'm very new to javascript (c++ normally) and I think this question should be quite basic for you all. 我对javascript(通常是c ++)非常陌生,我认为这个问题对您来说应该是非常基本的。

I have a script that gets a variable defined in an external .js file and displays it using an alert. 我有一个脚本,该脚本获取在外部.js文件中定义的变量并使用警报显示它。

The code in the .html file looks like this. .html文件中的代码如下所示。

<html>
<head>
<title></title>
<script> 

function addScript(url){
    var extScript = document.createElement('script');
    extScript.type = 'text/javascript';
    extScript.src = url;
    extScript.id = 'extScript'

    //If there is already a script with the ID 'extScript'
    //get rid of it
    var headList = document.getElementsByTagName('head');
    var scriptList = headList[0].getElementsByTagName('script');
    for(var i = 0; i < scriptList.length; i++)
    {
        if(scriptList[i].id =='extScript')
        {
            document.getElementsByTagName('head')[0].removeChild(scriptList[i]);
        }
    }

    document.getElementsByTagName('head')[0].appendChild(extScript);

}

function newNewChangeMode()
{
    addScript("C:/Users/Suzaku/Documents/Javascript/controller.js");
    alert("Neo controllerMode variable is reading " + controllerMode);
}

</script>
</head>
<body>
<a href="javascript:newNewChangeMode()">Get externally defined mode</a>
</body>
</html>

And the file "controller.js" looks like this. 文件“ controller.js”看起来像这样。

var controllerMode = 1111;

(that's it!) (而已!)

When I click the link "get externally defined mode", my javascript runs and the alert is displayed correctly. 当我单击“获取外部定义的模式”链接时,我的JavaScript运行,并且警报显示正确。 Displaying "Neo controllerMode variable is reading 1111". 显示“ Neo controllerMode变量正在读取1111”。

However, if I change the variable controllerMode's definition (in controller.js) to 但是,如果将变量controllerMode的定义(在controller.js中)更改为

 var controllerMode = 2222;

,hit save, and click the the button again (without refreshing), it still alerts "Neo controllerMode variable is reading 1111". ,点击保存,然后再次单击按钮(无需刷新),它仍会提示“ Neo controllerMode变量正在读取1111”。 Whereas it SHOULD say "Neo controllerMode variable is reading 2222". 应该说“ Neo controllerMode变量正在读取2222”。

It would seem that this script is not being added dynamically. 似乎该脚本没有被动态添加。 I need to be able to change this variable without having to refresh the .html. 我需要能够更改此变量,而不必刷新.html。

Thanks in advance, 提前致谢,

Guy 盖伊

It sounds like your browser is only retrieving the cached javascript file. 听起来您的浏览器只在检索缓存的javascript文件。 Make sure you go directly to your updated javascript file (controller.js) and hit F5 to ENSURE that you are loading a new version, not the cached one. 确保直接进入更新的javascript文件(controller.js),然后按F5键以确保您正在加载的是新版本,而不是缓存的版本。 Otherwise your browser will keep plugging the old, cached script (which has 1111 defined) into your script. 否则,浏览器将继续将旧的缓存脚本(已定义1111)插入脚本中。 Common problem! 常见问题!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM