OK, so I'm reading the contents of my data file, where I have stored display width, display height, image width, image height. I'm trying to store these values into variables, that I can use in other functions on my page. Having heard that global variables are a bad idea, I created this object:
var slidedata = {dw:null,dh:null,rw:null,rh:null}
I get to this part, within a function:
slidedata.dw = d[o].dispWidth; // How do I do this?
When I do alert(slidedata.dw);
, later on, I get null, meaning I haven't changed the value of slidedata.dw
. I checked, when I alert(d[o].dispWidth);
, I get an OK value (307), so that's not the problem. The problem is that I don't know how to change the value of slidedata.dw
.
HOW DO I DO THIS?
FULL CODE:
<script type="text/javascript">
var slidedata = {dw:null,dh:null,rw:null,rh:null}
function ajaxjson(folder){
var info = document.getElementById("info-wh");
var hr = new XMLHttpRequest();
hr.open("POST", "gallery_data.php");
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var d = JSON.parse(hr.responseText);
for(var o in d){
if(d[o].src == $('.image').attr('src')){
slidedata.dw = d[o].dispWidth;
// HERE I'M TRYING TO SET slidedata.dw TO BE d[o].dispWidth
// d[o].dispWidth RETURNS 307
slidedata.dh = d[o].dispHeight;
slidedata.rw = d[o].realWidth;
slidedata.rh = d[o].realHeight;
}
}
}
}
hr.send("folder="+folder);
}
</script>
</head>
<body>
<div id="info-wh"></div>
<div id="info-cd"></div>
<script>ajaxjson('slides');</script>
<script>alert(slidedata.dw);</script> <!-- THIS RETURNS NULL -->
<img class="image" alt="" border="0" src="slides/slide1.jpg"
style="width:400px; height:600px; left:200px; top:25px; position:relative;" />