如何在html和javascrip中声明全局变量

2024-06-06 07:53:33 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在html和javascript中为django模板页声明全局变量。我想让显示语言成为一个全局变量。

<script>
  function onChange(){
    if (xmlHttp.readyState==4 && xmlHttp.status==200) {
        //request is successful. So retrieve the values in the response
        display_language = xmlHttp.responseText.split(';');
        alert("response: " + display_language);
   }
}
 </script>

<html>
  <body>
     {% ifequal item.lang display_language %}
           {{item.text.strip}}
     {% endifequal %}
   </body>
</html>

Tags: thedjango模板语言声明responsehtmldisplay
1条回答
网友
1楼 · 发布于 2024-06-06 07:53:33

JavaScript中的变量是隐式全局的,因此除非它们在函数中并以var关键字作为前缀,否则它们将是全局可访问的

这是全球性的

<script type='text/javascript'>
    foobar = 'hello';
</script>

这也是全球性的

<script type='text/javascript'>
   function test() {
       foobar = 'hi';
   }
</script>

这是本地的

function test() {
    var foobar = 'world'; 
}

相关问题 更多 >