views:

91

answers:

3

I have a jQuery function that updates my data ever few seconds.

But! It's not parsing the javascript it's loading. It just calls it appropriately every few seconds, and replaces my content with unparsed javascript code.

setupMediaIndexPoller: function(organization) {
    url = '/organizations/' + organization + '/media/photos_and_video'
    $.PeriodicalUpdater(url, {
       method: 'get',          
       data: '',                  
       minTimeout: 20000,       
       maxTimeout: 60000,       
       multiplier: 2,          
       type: 'html',       
       maxCalls: 0,            
       autoStop: 0             
     }, function(data) {
         $('#media_index').html(data);
     });
}

I tried making the dataType: script, but that didn't help. Any ideas?

+3  A: 

You want to have this string evaluate as javascript? If so, use Javascript's eval function:

http://www.w3schools.com/jsref/jsref_eval.asp

treeface
thanks treeface. i'd hug you if i was a bigger hippie.
Trip
@Trip - please be very careful with the use of eval(). You need to be certain that any code sent to it has not been tainted in any way. While powerful, it can lead to a lot of unwanted XSS issues if not used properly.
Brian
Interesting. What else can I use?
Trip
There's lots of options on what else to use, just depends on how you're doing it. I tend to insert my elements into the dom with an `$(document).ready(function () { /* new exec code goes here */ });` flavor already embedded in them (where the block is generated serverside for me, instead of clientside), and I do an append by containing div then remove the previous container (using child selectors) ... works really well for me. No EVAL needed.
drachenstern
@dracenstern, the trouble with that is that Trip is making (what I assume to be) an asynchronous call to the server, after the browser has read all available Javascript. As long as Trip knows where his code is coming from and properly cleans for XSS (I imagine RoR does this automatically for user inputs?), using eval shouldn't be a problem. That said, I never use it myself because I haven't found a situation where it would be particularly useful, but for a highly-modular architecture, perhaps it would be.
treeface
Good point Treeface. RoR does sanitize XSS in inputs. I'll be sure to include it. Thanks everyone.
Trip
@Trip Why not just return straight html or json from your controller action?
Brian
@Brian the more I think about it, the more I can see a potential use for evaling javascript. Really the best scenario I can think of is if you have a **LOT** of JS and you want to load functionality in asynchronously on user request. So if you have a library or an object that is >100kb and you want to only load it when a user clicks on "maps" or something, eval() would probably save you some bandwidth overall, assuming not all users click on "maps". Pretty specific-use scenario, but I suppose anything's possible. Typically, I do what you say...pull back JSON results from the server.
treeface
Brian
A: 

Try a little of this, maybe?

type: 'get'
Joel Meador