DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simple JS Include Function

  • submit to reddit
        // Simple JS Include function... add a root.js to the base lib directory...

function $include(path)
var x, base, src = "root.js", scripts = document.getElementsByTagName("script");
for (x=0; x<scripts.length; x++){if (scripts[x].src.match(src)){ base = scripts[x].src.replace(src, "");break;}}
document.write("<" + "script src=\"" + base + path + "\"></" + "script>");


Snippets Manager replied on Wed, 2007/07/11 - 12:21pm

The problem with they way your using it that the included script won't get parsed untill the current script is finished being parsed. here is a better solution for realtime inclusion.