Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent but more idiomatic to eval( ) in jQuery?

Tags:

jquery

What's the equivalent to making this call eval('('+responseText+')') in jQuery? I also understand that eval is not that safe, so I'm looking for something safer and more idiomatic in jQuery.

like image 995
cathat Avatar asked Jan 27 '26 22:01

cathat


2 Answers

Are you actually looking to execute Javascript code (which will always be unsafe to some extent), or are you trying to parse JSON from a string into an object?

If the former, eval() is still going to be your best bet.

If the latter, jQuery will do it for you as part of an AJAX request, or you can use a dedicated JSON plug-in to convert between string and object without the risk of executing arbitrary code.

like image 140
Luke Dennis Avatar answered Jan 29 '26 13:01

Luke Dennis


a) To load and execute JS code:

$.getScript( url, [ success(data, textStatus) ] )

see http://api.jquery.com/jQuery.getScript/

b) To parse JSON:

$.parseJSON( json )

see http://api.jquery.com/jQuery.parseJSON/

like image 26
thorn0 Avatar answered Jan 29 '26 12:01

thorn0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!