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

03.27.2006
| 10802 views |
  • 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>");
}
    

Comments

Snippets Manager replied on Wed, 2007/07/11 - 1: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. http://www.exit12.org/archives/12