javascript全局变量未在javascript中设置 [英] javascript global variable doesn't set in javascript

查看:70
本文介绍了javascript全局变量未在javascript中设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对javascript很陌生.我在更改javascript全局变量的值时遇到麻烦.这是我的代码.

在header.php文件中,我声明了全局变量.

I''m very new to javascript. I have a trouble in changing the value of javascript global variable. Here is my code.

In header.php file I have declared the global variable.

<script type="text/javascript">
        var name;


</script>

然后在index.php文件中,更改名称"变量的值.


</script>

Then in index.php file I change the value of the "name" variable.

if (xmlhttp.readyState==4 && xmlhttp.status==200)
                                             {
                                                if(xmlhttp.responseText != null)
                                                    {
                                                 JSONObjList=xmlhttp.responseText;
                                                    if(JSONObjList!=null){
                                                         var JSONListObj = JSON.parse(JSONObjList);
                                                          if(JSONListObj.loggedIn == true){
//
                                                               name=JSONListObj.username+"|true";
                                                               alert(name);
                                                          }
                                                          else{
                                                              name="";
//                                                              logstatus="false";
                                                          }


                                                    }


                                                 window.location = "http://localhost/mvc/myplaylist";
                                                    }
                                                    else{
                                                  document.getElementById("myDiv").innerHTML="Login details are incorrect";
                                                  form.email.value="";
                                                  form.password.value="";
                                             }



在这里alert(name)给出了所需的值.然后,我要在header.php中再次访问该值,如下所示.



Here alert(name) gives the required value. Then I want to access that value again in header.php as in the following.

<script type="text/javascript">
                                             alert(name);
                               var arr=name.split("|");
                               if(arr != null)
                                   {

                                       var status=arr[1];
                                       var inputstr=arr[0];
                                   }
                               if(status =="true"){

                                       document.write("<td id='log' rowspan='2'onMouseOut='MM_swapImgRestore()' onMouseOver='MM_swapImage('Image4','','images/bttnn/btn_logout.png',1)' onclick='DestroySes()'><img src='images/bttnn/btn_logout_h.png' alt='Log Out' name='Image4' width='150' height='56' border='0'</td>");
                               }
                                else
                                      document.write("<td id='log' rowspan='2'><a href='#login_form' id='login_pop'onMouseOut='MM_swapImgRestore()' onMouseOver='MM_swapImage('Image4','','images/bttnn/btn_login.png',1)'><img src='images/bttnn/btn_login_h.png' alt='Log In' name='Image4' width='150' height='56' border='0'></a></td>");

                          </script>



上面的代码在Chrome上运行良好.但是当我在Firefox中运行它时,name值在header.php中什么都不会打印
谁能解释我的原因.



Above code works fine for Chrome. But when I run it in Firefox name value prints nothing in header.php

Can anybody explain me the reason.

推荐答案

代码在Firefox中不起作用bcz该属性不受Firefox支持,但支持chrome.

谢谢
尼基尔(Nikhil)
the code is not working in firefox bcz this property is not supported by firefox but chrome supported.

Thanks
Nikhil


这篇关于javascript全局变量未在javascript中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆