tags:

views:

28

answers:

3

Ok,

I have a some javascript code in the database

Table: jsSnippets

Field: snippet

Type: Text

<SCRIPT SRC="https://svc.com/somestuff.js"&gt;&lt;/SCRIPT&gt;
<script>
var fubar = 'stuf'
send_some_stuf_to_svc(fubar) // sends some data to a service :)
</script>

So i have N number of this JS snippets

will that code work if a server side method was called via Ajax call, for example:

$.ajax({
            type: 'GET',
            url: path + '/doTheJSStuff/',
            )};

where the doTheJSStuff is a method that echo/prints the JS code

+2  A: 

The response body of the AJAX invoked HTTP GET call to .../doTheJSStuff/ URI should contain HTML with the script code from the databse.

The browser will process this, but only if the Ajax call has the option set to "eval" script in the response to its call.

The script in the response needs at least one function call, which it has.

Lastly, and possibly not and issue for you, if the server the server you are making the ajax call to is different from the server the browser is looking at, you may need another solution (like JSONP) to get around security issues[http://en.wikipedia.org/wiki/Same_origin_policy].

Dara
+2  A: 
  1. Your script must return javascript code (without html tags).
  2. Call eval(text) after receiving text.
Riateche
+1  A: 

That will work, so long as the contents of the <script> tag are being passed into javascript's eval() function. If you're using a framework such as jQuery, its built-in $.ajax() method eval's tags automatically.

Joshua Burns