Using the .
js file to keep the javascript functions:
As you might have noticed up till now that when ever you embed the javascript in the StringBuilder object it will make it larger and larger. If you are using many javascript functions that it will be a good idea to save all the javascript functions in a single .
js file. This way all the code is present in one place and it will be easy to make any changes.
Let's see that how we can make the use of the .
js files to save our code:
First add the .
js file which you will use in your application. Adding a script file is very simple you can just right click on the project and select add new item and at the bottom you will see a javascipt file just add that file and name it as "MyJavaScriptFile".
if(!Page.IsPostBack)
{
if(!Page.IsClientScriptBlockRegistered("MyScript") )
{
Page.RegisterClientScriptBlock("MyScript","<SCRIPT Language='JavaScript' src='MyJavaScriptFile.
js'></SCRIPT>");
}
Button1.Attributes.Add("onclick","PopWindow();");
}
We have done this step in the previous examples. Here a little bit has changes now we are registering the javafile which is the .
js file. And also remember that .
js file only contains the functions and not the whole list of javascript tags.
Let's take a lot at a simple .
js file:
function PopWindow()
{
alert('hello world from the Javascript file');
}
As you can see that the PopWindow() is the same as we have used before. By using this technique we have saved lot of bad coding and lot of code writing in the code behind file. You should always use the .
js file to save your javascript.
From :
Himanshu Sharma