I have a javascript function that I write that is used to include an external JS file, but only once. The reason I need such a function is because it is called when some content is loaded via AJAX, and I need to run the page-specific code for that content (no, just use .livewon't cover it).
Here is my attempt, shortened for brevity:
$.include_once = function(filename) {
if ($("script[src='" + filename + "']").length === 0) {
var $node = $("<script></script>")
.attr({
src : filename,
type : "text/javascript"
})
;
$(document.body).append($node);
}
};
This works fine: the function is called, it loads the external file, and this file is launched at boot. Perfect.
The problem is that it will always reload this external file: the query that I use to check for the existence of the script always finds nothing!
When debugging, I added a few lines:
alert($("script").length);
$(document.body).append($node);
alert($("script").length);
( HTML Firebug) script .
, , , , ( ) , JS .
- , ?