views:

60

answers:

4

I have a site that loads information using the XMLHttpRequest when a user clicks a link. The system works well but I would like to be able to execute JavaScript gathered in this process.

This is a problem as I would like to download the scripts 'on demand' if it were, rather than loading them all when the page is loaded.

Thanks for any help

+1  A: 

I believe the recommended solution is something like this:

function include(scriptUrl)
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", scriptUrl);
    xmlhttp.onreadystatechange = function()
    {
        if ((xmlhttp.status == 200) && (xmlhttp.readyState == 4))
        {
            eval(xmlhttp.responseText);
        }
    };
    xmlhttp.send();
}

Or something like it.

However, be wary of this approach. It's vulnerable to cross-site scripting, which can open you (and your users) up to all sorts of nastiness. You'll want to take suitable precautions.

Mike Hofer
Didn't see your comment when posted. You're completely right. However I think you have to check readyState value in onreadystatechange handler like it is shown here http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp.
Kirill Muzykov
@Insomniac: Duly noted, and thanks. Post fixed! Wouldn't work very well if the stream ended in the middle of a statement now, would it?
Mike Hofer
Kirill Muzykov
And even better to use something like thishttp://plugins.jquery.com/project/rloader to avoid eval'ing.
Kirill Muzykov
While I like jquery, personally, it's not suitable for every environment. I'd personally have my lips stapled to the wall if I introduced it into the product at work. So, you show folks both approaches and enjoy the benefits of increased knowledge.
Mike Hofer
@Insomniac: You caught me again. Curses! Foiled again! :-P
Mike Hofer
No worries, I paste unfinished code all the time, when I want to demonstrate idea :)
Kirill Muzykov
A: 

You would need to use eval to parse the javascript from the XHR, note that this is EXTREMELY dangerous if you don't have absolute trust in the source of the javascript.

JaredM
+1  A: 

You can run script downloaded in form of a string using

eval()

However I would recommend you to add new

<script src='..'></script>

to your document and have a callback which will be called when it will be downloaded. There are many utils and jquery plug-ins for that.

Kirill Muzykov
`eval`'ing from XHR is less dangerous than injecting a `<script>` due to same-origin policies placed on XHR (which are not present with `<script>`). However, setting up `<script>` is often simpler.
strager
I totally agree that both approaches have a security issues, but if you want dynamic script loading you don't have too much to choose from :) Also eval()'ed script are not cached and can badly affect performance if you have a big script.
Kirill Muzykov
A: 

You would use eval(param) and the parameter would be the response from the request. Your method of requesting for JavaScript is ridiculous as it only delays the code from running. Just use function and events.

ApocalypeX