Jump to content

Unable To Execute Compound Statement, Help !


q8tracker

Recommended Posts

Hi all ,i have "Literally" spent my last 24 hours trying to figure out how to do this part of code, i have a poor experience in java scripting but i need this to be done.i have a changeable string (xmlhttp.responseText) which i need it to be executed in my codexmlhttp.responseText = var point = new GLatLng(51.4296616667, 0.05517666667); var marker = new GMarker(point); map.addOverlay(marker); GEvent.trigger(marker, "click"); map.setCenter(point, 14); // for nowand i don't want to do this:{var point = new GLatLng(51.4296616667, 0.05517666667);var marker = new GMarker(point);map.addOverlay(marker);GEvent.trigger(marker, "click");map.setCenter(point, 14);}since its changing i need to call it every time by value using compound statementi want to do it like this :{ xmlhttp.responseText } // its changing every call, contains dynamic variablesi even tried {( xmlhttp.responseText) } and { (xmlhttp.responseText);} but unfortunately non of them seems to be workingit is working when using a PHP variable like: {$placeicon_html}but this is not what i want since the php string is static ( server side ) and i have another dynamic part of code which i need to be executed at specific time intervalsmost i found about compound statement is nearly to this: http://php.comsci.us/syntax/statement/compound.php and i couldn't find out how to execute it using strings !!any help please :Sthanks in advance ..

Link to comment
Share on other sites

I'm not sure I see the idea... you have what in xmlhttp.responseText that is to be evaluated? JavaScript code? The eval() function would work if that's the case. Every time you have a new string, you can eval() it, which would execute it.Simply writing

{ xmlhttp.responseText }

doesn't do anything alone. If it's in a function that mut return a result, use the return keyword:

function() { return xmlhttp.responseText }

If xmlhttp.responseText must be provided as a function, this structure is not needed, e.g.

eval(xmlhttp.responseText)

If you want to change the value of xmlhttp.responseText... don't. Create another variable, and assign the value from xmlhttp.responseText if you must, but don't try to change xmlhttp.responseText.Perhaps I don't understand your point still... please explain the goal.

Link to comment
Share on other sites

In any case, this will not work in javascript:x = var y;You can declare something a var when it is an l-value (left of the assignment operator) but not when it is an r-value. You can chain assignments, as in a = b = c = 5, but that is a slightly different matter.And there is absolutely no advantage to punctuating a series of statements on one line instead of multiple lines. You do gain a very small advantage if you declare new variables with a single var keyword, as the following two examples show:

var a, b, c, d = 2, e = getValue();

Which is the equivalent of this:

var a,	b,	c,	d = 2,	e = getValue();

Notice the commas instead of semicolons. Each variable in these cases in declared as a var. The second is easier to read, however, at the expense of a few characters.

Link to comment
Share on other sites

thanks for your concern guys, but its not working :/let me try to simplify my goal ..im working a google map api on my web page for a tracking project where the content of the map needs to be updated every 5 seconds.im sending me GPS data from my tracker to a txt file in the server and then have a PHP script to read that file, fetch data and then constitute a JavaScript code using data provided, that means that i have new part of code every 5 seconds which needs to be executed.Finally im accessing that PHP script using AJAX and being able to read it and print it out using (document.Write) or (Alert ) but what i need to do is EXECUTE IT!!its here available readable but unfortunately NOT executable :/ xmlhttp.responseText is my AJAX variable. which is being updated every 5 seconds.i need to execute this ( xmlhttp.responseText ) string which holds code like( var point = new GLatLng(51.4296616667, 0.05517666667);var marker = new GMarker(point);map.addOverlay(marker);GEvent.trigger(marker, "click");map.setCenter(point, 14); )and its changinghow can i do this ??

In another way for example :X = alert('hello world');How can i execute X ??{ X } is not working .. and as i said in PHP when doin { $X } its working ..

Link to comment
Share on other sites

how are you outputting the new values?try creating a php file that will echo the javascript so response can execute it. you can read the text file and use php to change the values.should be similar to this.<?phpecho "<script type=\"text/javascript\">\n";echo "(var point=new GLatLng(51.4296616667, 0.05517666667);var marker=new GMarker(point);map.addOverlay(marker);GEvent.trigger(marker, \"click\");map.setCenter(point, 14):)";echo "</script>\n";?>

Link to comment
Share on other sites

Finally im accessing that PHP script using AJAX and being able to read it and print it out using (document.Write) or (Alert ) but what i need to do is EXECUTE IT!!
If all you have is Javascript code, without any HTML markup, then eval is the correct way to execute a string of text as code. You could also create a new script element, add your code as the innerHTML of the script element, and put the script element on the page. That may work also. But in general, returning Javascript from the server and just trying to put it on the page will not actually execute the code.
Link to comment
Share on other sites

Oh, so PHP returns something like

var point = new GLatLng(51.4296616667, 0.05517666667);var marker = new GMarker(point);map.addOverlay(marker);GEvent.trigger(marker, "click");map.setCenter(point, 14);

and you need to execute that?OK... that's (relatively) easy...Set up a setInterval() function in which at every five seconds you do

xmlhttp.send(null);

At your onreadystatechange function (which you must prepare before the first send()), when the request is received (xmlhttp.readystate == 4), do

eval(xmlhttp.responseText);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...