Skip to content Skip to sidebar Skip to footer

Getscript Or Eval In Specific Location?

I was wondering if eval (or some variant of jQuery's getScript) can be used to position external javascript in places other than the end of the DOM or at the head. I've tried: var

Solution 1:

Does the "JavaScript toolkit" you refer to use document.write or document.writeln to insert output into the page? If so, you could override that function to append the script output into the correct location:

document.write = function(s) {
    $('#fig').append(s);
};

document.writeln = function(s) {
    $('#fig').append(s + '\n');
};

and then load and execute the script using $.getScript.

Edit: A more robust way of doing it, depending on how the code is added:

var output = '';

document.write = function(s) {
    output += s;
};

document.writeln = function(s) {
    output += s + '\n';
};

$.getScript('URL of script here', function() {
    $('#fig').append(output);
});

Post a Comment for "Getscript Or Eval In Specific Location?"