Hey I have a json_encode which im trying to loop and display the output but its not working, i get no output =/
This is my js fiddle: http://jsfiddle.net/Z4jTc/1/
Can some one explain why it won't loop?
Code:
var messages = {"1":["2","1","0000-00-00 00:00:00","testing once"],"2":["2","1","0000-00-00 00:00:00","testing twice :)"]};
for(var id in messages){
var read = messages[id][0];
var from = messages[id][1];
var sent_on = messages[id][2];
var content = messages[id][3];
document.write = 'test';
document.write = '<div class="message_info">';
document.write = '<b>From:</b> '+from+'<br/><br/>';
document.write = '<b>Sent On:</b> '+sent_on;
document.write = '</div><div class="message_content">';
document.write = content;
document.write = '</div> <br/><br/>';
}
You are constantly overwriting the document.write
function, you are never actually calling it.
document.write("test");
is how it's supposed to be used.
So many document.write's is pretty slow. You could do it in one call after you parse the object:
var messages = {"1":["2","1","0000-00-00 00:00:00","testing once"],"2":["2","1","0000-00-00 00:00:00","testing twice :)"]};
var output = [];
for(var id in messages){
var read = messages[id][0];
var from = messages[id][1];
var sent_on = messages[id][2];
var content = messages[id][3];
output.push('test');
output.push('<div class="message_info">');
output.push( '<b>From:</b> '+from+'<br/><br/>');
output.push('<b>Sent On:</b> '+sent_on);
output.push('</div><div class="message_content">');
output.push(content);
output.push('</div> <br/><br/>');
}
document.write(output.join(''));
...or place the output in a container on the page...
document.getElementById('destinationContainer').innerHTML(ouput.join(''));
document.write
is a function
Use
document.write('test');
Instead of
document.write = 'test';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.