javascript - SyntaxError: expected expression, got ')' -
i've been stuck @ error few days , still couldn't figure out wrong. great if point me right direction of solving issue.
update: realise error gone when commented "addmessages(xml)" in updatemsg() function. how make work then?
error: http://i.imgur.com/91hgtpl.png
code:
$(document).ready(function () { var msg = $("#msg"); var log = $("#log"); var timestamp = 0; $("#name").focus(); $("#login").click(function() { var name = $("#name").val(); if (!name) { alert("please enter name!"); return false; } var username = new regexp('^[0-9a-za-z]+$'); if (!username.test(name)){ alert("invalid user name! \n please not use following characters \n `~!@#$^&*()=|{}':;',\\[\\].<>/?~@#"); return false; } $.ajax({ url: 'login.php', type: 'post', datatype: 'json', data: {name: name}, success: function() { $(".login").hide(); } }) return false; }); $("#form").submit(function() { if (!msg.val()) { return false; } $.ajax({ url: 'add_message.php', type: 'post', datatype: 'json', data: {message: msg.val()}, }) msg.val(""); return false }); window.setinterval(function () { updatemsg(); }, 300); function updatemsg() { $.post('server.php', {datasize: '1024'}, function(xml) { addmessages(xml); }); } function addmessages(xml) { var json = eval('('+xml+')'); $.each(json, function(i, v) { tt = parseint(v.time); if (tt > timestamp) { console.log(v.message); appendlog($("<div/>").text('[' + v.username + ']' + v.message)); timestamp = tt } }); } function appendlog(msg) { var d = log[0] var doscroll = d.scrolltop == d.scrollheight - d.clientheight; msg.appendto(log) if (doscroll) { d.scrolltop = d.scrollheight - d.clientheight; } } });
it might read on eval bit. looks doesn't think does.
eval() dangerous function, executes code it's passed privileges of caller.
also
there safer (and faster!) alternatives eval() common use-cases.
it looks you're trying data server in form of json. you'll need make sure server returns valid json, can verify here. server-side programming languages have library turn object json make piece of cake. here's example php.
on client-side, you'll need change var json = eval('(' + xml + ')');
var json = json.parse(xml);
give javascript version of php/perl/python/etc object. if it's array, can iterate through for
loop, array.prototype.foreach
, or variety of functions different libraries, such $.each
or _.each
.