How to automaticaly load a javascript file when a page is loadingThis is an advance functionality for web form developers.
1.Configure the custom IHttpModule in web.configYou have to add a custom IHttpModule in your web.config first.
Example: <httpModules>
<add name="ScriptLoaderModule" type="LangZi.Scripts.ScriptLoaderModule,LangZi.ScriptLoader"/>
</httpModules> 2.Rename or create your script fileIf the name of your webform page is "WebForm1.aspx", you must rename or create a script file with "WebForm1.aspx.js".
Example: -MyWebApp
- Scripts
- ...
- Styles
- ...
- WebForm1.aspx
- WebForm1.aspx.js And the "WebForm1.aspx.js" will be loaded automatically when the "WebForm1.aspx" is requested.
3. Use a syntactic sugar to load other script modulesIn the "WebForm1.aspx.js" , there is a special function "using(modulename)".It is the same as loader.Load(string name) .
Example: in the configuration file
<?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> in the "WebForm1.aspx.js"
using("MyScript");
//write your code
So, the "WebForm1.aspx.js" will be loaded automatically when the "WebForm1.aspx" is requested. And the "lib/jquery.min.js, demo2.js, myscript1.js" will be loaded automatically first when the "webFrom1.aspx.js" is loaded.
|