I was seeking to avoid the same thing recently. Instead of adding more JavaScript, I decided to use PHP. While we're using Rails, it's very inefficient to fire up the whole engine to do something as simple as outputting a script with a few interpolated variables. A simple PHP script can put out 2-3 times as many requests per second as Rails/Django for a task like that.
Simple Way to Extract GET Params from a JavaScript Script Tag
11–20 of 26 posts
Re: Simple Way to Extract GET Params from a JavaScript Script Tag
#12Re: Simple Way to Extract GET Params from a JavaScript Script Tag
#13 var getErrorLocation = function (error) {
var loc, replacer = function (stack, matchedLoc) {
loc = matchedLoc;
};
if ("fileName" in error) {
loc = error.fileName;
} else if ("stacktrace" in error) { // Opera
error.stacktrace.replace(/Line \d+ of .+ script (.*)/gm, replacer);
} else if ("stack" in error) { // WebKit
error.stack.replace(/at (.*)/gm, replacer);
loc = loc.replace(/:\d+:\d+$/, ""); // remove line number
}
return loc;
};
You could use it as such: try {
0();
} catch (e) {
var scriptLocation = getErrorLocation(e);
}
In SpiderMonkey and Rhino, the Error constructor itself is magic too, so you could just do getErrorLocation(new Error) instead.Re: Simple Way to Extract GET Params from a JavaScript Script Tag
#14This solution fails when you need to insert the script more than once in a page. Instead, browsers really should provide scripts a means to retrieve the script element that they belong to.
Re: Simple Way to Extract GET Params from a JavaScript Script Tag
#15Here's a simpler way. Put this in your htaccess file: AddHandler server-parsed .js then add: (function(query_string){ ... })(" "); around your script. This obviously has problems with caching, but it doesn't have the problems addressed with those "heavy loads" that you have with PHP et al.
Re: Simple Way to Extract GET Params from a JavaScript Script Tag
#16Re: Simple Way to Extract GET Params from a JavaScript Script Tag
#17example usage:
code is here: http://script.aculo.us/scriptaculous.js
Re: Simple Way to Extract GET Params from a JavaScript Script Tag
#18This solution fails when you need to insert the script more than once in a page. Instead, browsers really should provide scripts a means to retrieve the script element that they belong to.
scripts=document.elements.getElementByTagName('script')
latest=scripts[scripts.length-1]Re: Simple Way to Extract GET Params from a JavaScript Script Tag
#19This solution fails when you need to insert the script more than once in a page. Instead, browsers really should provide scripts a means to retrieve the script element that they belong to.
A comment on the article pointed out that since javascript execution is blocking, you can do that for synchronous javascript: scripts=document.elements.getElementByTagName('script') latest=scripts[scripts.length-1]
This approach allows you to specify parameters as JSON. So data of arbitrary length can be passed without having to resort to some global variable.
It solves a problem I had where I needed to pass some template engine generated content into an otherwise static script.
Thanks for the tip :)