I am using $.post to load some js code from a MYSQL database. How do I execute it?
-
2You've stored a jquery code in your MySQL database?Darin Dimitrov– Darin Dimitrov2011-02-19 22:01:21 +00:00Commented Feb 19, 2011 at 22:01
-
2you put your js code into you mysql database? why?Luke– Luke2011-02-19 22:02:21 +00:00Commented Feb 19, 2011 at 22:02
-
stackoverflow.com/questions/3146069/…Luca Filosofi– Luca Filosofi2011-02-19 22:08:39 +00:00Commented Feb 19, 2011 at 22:08
-
Here's a guess as to why you would store javascript code in a database: that makes it 1) accessible from multiple applications, and 2) easy to maintain.DOK– DOK2011-02-19 22:09:02 +00:00Commented Feb 19, 2011 at 22:09
-
@Darin Dimitrov, @Luke: True, storing the code in the database might not be necessary, but we don't know the context. I'm pretty sure jsfiddle.net uses a database as well ;)Felix Kling– Felix Kling2011-02-19 22:12:12 +00:00Commented Feb 19, 2011 at 22:12
|
Show 6 more comments
2 Answers
You can use $.ajax (instead of $.post) with the the dataType option set to script:
"script": Evaluates the response as JavaScript and returns it as plain text. Disables caching unless option "cache" is used. Note: This will turn POSTs into GETs for remote-domain requests.
2 Comments
Raynos
You forgot to mention he probably doesnt want to load snippets of code from his database.
Felix Kling
@Raynos: I think this was mentioned often enough.
$.eval = function(str) {
eval(str);
}
$.fn.eval = function(str) {
eval(this.selector);
}
$.post(url, function(data) {
$.eval(data);
//$(data).eval();
});
Jokes aside. You can use eval to run a snippet of JavaScript. I'm sure everyone else will tell you why eval is evil.
Ideally though what your doing is bad. There's a far better way to solve your problem that doesn't involve grabbing code from a database.
1 Comment
Dykam
Big remark. The words eval and evil only differ one letter. For a reason.