Are there any alternatives to using eval
to immediatly run remote & trusted javascript code.
function load(filePath) {
var o = $.ajax({
url: filePath,
dataType: 'html',
async: false
});
eval(o.responseText);
}
load("somePath");
// run a function that relies on the code from o.responseText being loaded
doSomethingWithCode();
I'm aware that synchronous loading of javascript is adviced againts. But if there is no choice are there any cross browser alternatives for the use of eval above.
[Edit]
To clarify in more detail the code being loaded is a self executing function. Which needs to execute before doSomethingWidthCode. It's also being loaded from the server on the same domain hence its trusted.
Are there any alternatives to using eval
to immediatly run remote & trusted javascript code.
function load(filePath) {
var o = $.ajax({
url: filePath,
dataType: 'html',
async: false
});
eval(o.responseText);
}
load("somePath");
// run a function that relies on the code from o.responseText being loaded
doSomethingWithCode();
I'm aware that synchronous loading of javascript is adviced againts. But if there is no choice are there any cross browser alternatives for the use of eval above.
[Edit]
To clarify in more detail the code being loaded is a self executing function. Which needs to execute before doSomethingWidthCode. It's also being loaded from the server on the same domain hence its trusted.
Share Improve this question edited Jan 5, 2011 at 14:37 Raynos asked Jan 5, 2011 at 14:21 RaynosRaynos 170k57 gold badges357 silver badges398 bronze badges 2- 1 You say the code is trusted, so what are you worrying about? – Anders Commented Jan 5, 2011 at 14:25
- @Anders eval ruins my debugging abilities. – Raynos Commented Jan 5, 2011 at 14:27
4 Answers
Reset to default 4Dynamic script text insertion is the only alternative to eval
.
var head = document.getElementsByTagName('head')[0] || document.documentElement,
nscr = document.createElement('script');
nscr.type = 'text/javascript';
nscr.textContent = o.responseText;
nscr.setAttribute('name', 'dynamically inserted');
nscr.onload = nscr.onreadystatechange = function() {
if( nscr.readyState ) {
if( nscr.readyState === 'plete' || scr.readyState === 'loaded' ) {
nscr.onreadystatechange = null;
doSomethingWithCode();
}
else {
doSomethingWithCode();
}
};
head.insertBefore(nscr, head.firstChild);
Only thing to mention: textContent
is not available in InternetExplorers. You would need to use .text
instead there, so a little detection for that makes it cross-browser patible.
edit
To have a syncronous
loading dynamic script tag, you could add nscr.async = true;
. Anyway, this only works in cutting edge browsers.
I would use JSONP in this case. Raymond Camden provides and excellent introduction to the concept.
A quick example of using JSONP in this situation is available at http://playground.itcouldbe9./syncjsonp/.
You can have your code returned wrapped inside a function, and when the request pletes, execute that function. For example, this is your remote code:
function hi(){alert('hi');}
And then when your request is plete, you can inject that code into a javascript tag and then call the function hi()
why not use a callback?
eval('(function(){' + o.responseText + ';})(); doSomethingWithCode();')
EDIT:
OK then try this:
var o = $.ajax({
url: filePath,
dataType: 'html',
async: false
success: function(responseText){
eval('(function(){' + responseText + '})()');
doSomethingWithCode();
});
});
I think the only option left would be polling:
(function(){
if (o.responeText)
doSomethingWithCode();
else
setTimeout(arguments.callee, 13);
})();