1. Add a reference to LangZi.ScriptLoader.dll

Please add a referenct to LangZi.ScriptLoader.dll in your visual studio solution.

2. Add the custom httphanlder in web.config

Register the custom IHttpHanlder LangZi.Scripts.ScriptLoaderHandler,LangZi.ScriptLoader in your web.config

Example:

 <httpHandlers>
     <add verb="GET" path="*.js" type="LangZi.Scripts.ScriptLoaderHandler,LangZi.ScriptLoader"/>
 </httpHandlers>

3. Configure a script file as a loadable module

A script must be configured in a configuration file (.jsconfig) before loading it.

The configuration file is a xml file which suffix is .jsconfig(i.e lib.jsconfig).

The structure of a jsconfig is like below:

Example:

<?xml version="1.0" encoding="utf-8" ?>
<Scripts>
    <Script Name="MyScript" Inherit="myscript1.js">
      <References>
        <Reference Name="demo2"></Reference>
      </References>
    </Script>
    <Script Name="demo2" Inherit="demo2.js">
      <References>
        <Reference Name="jquery"></Reference>
      </References>
    </Script>
  <Script Name="jquery" Inherit="lib/jquery.min.js">
  </Script>
</Scripts> 
4. Register an instance of ScriptLoader

A. Register an instance at server side (recommended)

An instance of ScriptLoader is required when you want to load some script module. LangZi.Scripts.ScriptLoader.RegisterInstance(Page host) should be called when you want to create an loader instance.

Example:

protected void Page_Load(object sender, EventArgs e)
{
   LangZi.Scripts.ScriptLoader loader = LangZi.Scripts.ScriptLoader.RegisterInstance(this);           
}
B. Register an instance at client side

Import the boot script in the header of a page.

Example
   <script src="LangZi.Scripts.boot.js" charset="UTF-8"  type="text/javascript"></script>  

5.Load some script module

A. At the server side (c#),you can call the loader.Load(string name) method to load a script

Example:
protected void Page_Load(object sender, EventArgs e)
{
   LangZi.Scripts.ScriptLoader loader = LangZi.Scripts.ScriptLoader.RegisterInstance(this);
   loader.Load("MyScript");
}
B. At the client side (js) ,you can call the ScriptLoader.Load(name) method to load a script

Example:
<script type="text/javascript">  
 ScriptLoader.Load("MyScript");  
</script> 

The scripts ("lib/jquery.min.js","demo2.js","myscript1.js") will be loaded sequentially when the page is rendering.

Last edited Apr 8, 2009 at 7:11 AM by cnlangzi, version 3

Comments

sach Sep 13, 2009 at 10:52 AM 
nice stuff really nice