0

I am using $.post to load some js code from a MYSQL database. How do I execute it?

11
  • 2
    You've stored a jquery code in your MySQL database? Commented Feb 19, 2011 at 22:01
  • 2
    you put your js code into you mysql database? why? Commented Feb 19, 2011 at 22:02
  • stackoverflow.com/questions/3146069/… Commented 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. Commented 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 ;) Commented Feb 19, 2011 at 22:12

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You forgot to mention he probably doesnt want to load snippets of code from his database.
@Raynos: I think this was mentioned often enough.
1
$.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

Big remark. The words eval and evil only differ one letter. For a reason.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.