Have been trying to set a global var in javascript to be access from another function,
but there is a particular global var that I can't access, it returns null.
It works if i declare the var in the function itself.
Note: I am unable to use any js library.
This does not works:
var xhttp = new XMLHttpRequest(); <-- this works as the request in the function runs.
var DialogBody = document.getElementById("DialogBody");//<-- this is the problem
function fetchDialog() {
url = "../message/user_status.php";
xhttp.open('POST',url,true);
xhttp.send();
xhttp.onreadystatechange = function() {
if(xhttp.readyState == 4 && http.status == 200) {
showOverlay();
console.log("OK");// returns ok.
/*below all return error*/
DialogBody.innerHTML = "TEST";//<-- error: Cannot set property 'innerHTML' of null
}
}
}
This works:
function fetchDialog() {
url = "../message/user_status.php";
var DialogBody = document.getElementById("DialogBody");
xhttp.open('POST',url,true);
xhttp.send();
xhttp.onreadystatechange = function() {
if(xhttp.readyState == 4 && http.status == 200) {
showOverlay();
console.log("OK");// returns ok.
DialogBody.innerHTML = "TEST";//This works.
}
}
}
So my question is, how do I set that var as global instead of in function.
EDIT:(Solves by links provided by SO user.) <- not what I want.
var xhttp = new XMLHttpRequest();
var DialogBody;
document.addEventListener("DOMContentLoaded", function(event) {
DialogBody = document.getElementById("DialogBody ");
});
function fetchDialog() {
url = "../message/user_status.php";
xhttp.open('POST',url,true);
xhttp.send();
xhttp.onreadystatechange = function() {
if(xhttp.readyState == 4 && http.status == 200) {
showOverlay();
//console.log("OK");
DialogBody.innerHTML = "TEST";
}
}
}
UPDATE:(What I actually wanted.)
A little not so pretty workaround is this though it might be frown upon.
What I was actually trying to achieve is to set global var (consolidated).
I end up using eval() to set those var as string to achieve what I want and I find myself it very useful.
javascript concept
/.....
var xhttp = new XMLHttpRequest();
DialogBody = 'document.getElementById("DialogBody")';
//or DialogBody = 'document.getElementById("DialogBody").innerHTML';
function fetchDialog() {
url = "../message/user_status.php";
xhttp.open('POST',url,true);
xhttp.send();
xhttp.onreadystatechange = function() {
if(xhttp.readyState == 4 && http.status == 200) {
showOverlay();
//console.log("OK");
eval(DialogBody).innerHTML="Test";
//or eval(DialogBody) = "a message that is dynamically generated";
//eval(DialogBody).className="Whatever";
}
}
}
actual scenario.
user_status.php
//..query ends.
<div id="DialogTitleMessage">
<?php echo $DialogTitle;?>
</div>
<div id="DialogBodyMessage">
<?php echo $DialogTitle;?>
</div>
actual javascript
//.....
var xhttp = new XMLHttpRequest();
DialogBody = 'document.getElementById("DialogBody")';
DialogTitle = 'document.getElementById("DialogTitle")';
DialogTitleMessage = 'document.getElementById("DialogTitleMessage")';
function fetchDialog() {
url = "../message/user_status.php";
xhttp.open('POST',url,true);
xhttp.send();
xhttp.onreadystatechange = function() {
if(xhttp.readyState == 4 && http.status == 200) {
showOverlay();
eval(DialogBody).innerHTML = xhttp.responseText;
eval(DialogTitle).innerHTML = eval(DialogTitleMessage).innerHTML;
}
}
}