Jump to content

Execute Code Within A String


bakunin

Recommended Posts

I have one xml file and two xsl file, i parse them, one give me the HTML to show, one give me the Javascript to execute. I can do that in PHP with no problem, i echo the result of the two xslt, however i have to reload my page evry time i use it. The problem is when i do that in javascript, i can put the html within a div via dom with no problem, however the javascript who is in a string cannot be executed.here is a sample:var stringA = " $('#myitem').hide(); ";i have to execute the content of stringA.I don't know how to execute or echo it in java. I tried eval() but it does nothing.

Link to comment
Share on other sites

eval(stringA) should do it. A few possible problems strike me up front:1, you may be trying to access 'myitem' before that element is actually written into the DOM. By any chance are you trying to execute eval() in the global space of your script, before the rest of the page has loaded?2. Similarly, is 'myitem' an element that you are creating/modifying with JavaScript DOM methods? Are you 100% certain it is being created correctly, so that the id really gets assigned to it? The Firefox DOM Inspector can help you there.3, Perhaps the document that your PHP is generating does not look exactly as you think it does. Multiple levels of internal quotation marks can get messed up, for example.4, Just to be thorough: It looks like you're using a framework like JQuery or Prototype. Are you sure it's loaded before you try to execute $() ?When you run eval(), are you getting any messages in your Firefox Error Console?We'd all know more if you could paste a link to the page, or simply pasted the generated document here (not the PHP source document, at least not yet).

Link to comment
Share on other sites

Thanks for the reply, Yes its a possibility that the item does not already exist but i think the php code is executed first.What im trying to do is to generate a javascript code (mostly JQuery) with xslt, it does work well in php but in javascript (ajax), i cannot because i can't execute the result of that xslt wich ends up in a string. im pretty sure i could make my example work with eval() if i would try harder but there is one thing i wonder about that function. If my code is related to an event for sample something like this:$('#myitem').click(function(){ $('#myitem').hide(); ";}would it work or does eval() just read the code like a book and once the function is over, the code is erased from memory therefore not compatible with event.

Link to comment
Share on other sites

evaluating a string is the same as executing the code in the string. If the string creates a variable and assigns a value to it, then the rest of your script can now access that value through the variable. Same with a function assignment. The consequences are not temporary.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...