CoderBlog.NET
Tengiz Tutisani's blog.

 
Useful Links:
 
LanguageBox 
 
Language

 
SearchBox 
 
 
ArchiveBox 
 
Archive
<February 2012>
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
 
CategoriesBox 
 
Categories
  ASP.NET (5)
  Ajax (1)
  C# (7)
  Common (1)
  HTML (3)
  JavaScript (5)
  Products (1)
  SEO (1)
  SQL (1)
  Visual Studio (1)
  Windows (1)
 
FriendSitesBox 
 
Friend Sites
  AdMoney Project
  Cайт о гитаре и музыке!
  Design Studio IMAGINE
 
CustomContentBox [1] 
 



 
 
How to dynamically include JavaScript script reference into html
JavaScript’s day
Posted By:   tengo
At:   5/14/2009 5:25:00 PM
 

One of my friends asked me how to dynamically include a reference to the JavaScript file into an html file.
Sincerely to say, I didn't know what to answer. But no additional effort was needed to find the answer, it's easy.
If you are familiar with JavaScript, then you can think of it yourself and you'll find the answer.

I wrote a function in JavaScript which makes all the work: dynamically creates object corresponding to the "<script>" tag, assigns it's "src" property a value of file name, and then adds this object to the current document. Have a look:

<script language="javascript" type="text/javascript">
function IncludeJS(file)
{
var scr = window.document.createElement("script");
scr.src = file;
document.body.appendChild(scr);
}
</script>


All works fine. One more thing I would mention here:
When invoking a method that is contained in dynamically referenced script file, always make a little more coding to ensure that no script errors will be thrown. For this purpose, my test button's "onclick" event checks, if the method that SHOULD be in the included file, exists. Look at this button tag:

<input type="button" value="Invoke Sample Method" onclick="if (window.SampleMethod) SampleMethod()" />

That's all about today's JavaScript.


Filed Under:   JavaScript | HTML |
Post Tags:   dynamically | file | function | html | include | javascript | js | script | src |
  Comments [0]



 
CustomContentBox [2] 
 
Useful Links:
 
CategoryViewBox 
 
CoderBlog.NET
  Get your own Blog Engine
  SMS Sending Service
  What do you want to see on CoderBlog.NET?
  IMAGINE.GE created a header image for CoderBlog.NET
  CoderBlog.NET offers link exchange
  Quick language selection added
  CoderBlog.NET ‘s what is what
  Welcome to CoderBlog.NET

  Jump to Category...
 
TagsBox 
 
Popular Tags
aspnetblogcsharpdesigndivengineerrorformhtmljavascript
 
AuthorsBox 
 
Authors
  Tengiz Tutisani  (25)
 
SubscriptionsBox 
 
Subscriptions
Your Email:
 
LoginStatusBox 
 
Site Members
Login
 
Copyright © 2009 . CoderBlog.NET