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>
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="";
}
}
window.location = "http://localhost/mvc/myplaylist";
}
else{
document.getElementById("myDiv").innerHTML="Login details are incorrect";
form.email.value="";
form.password.value="";
}
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>
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.