Wrox Programmer Forums
|
BOOK: ASP.NET Website Programming Problem-Design-Solution
This is the forum to discuss the Wrox book ASP.NET Website Programming: Problem - Design - Solution, Visual Basic .NET Edition by Marco Bellinaso, Kevin Hoffman; ISBN: 9780764543869
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET Website Programming Problem-Design-Solution section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 9th, 2003, 06:23 AM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using js file in ASP.NET

dear friends,

i have created a javascript file (.js).how to use it in my asp.net application.

please explain with an example.

thanks,
ram kumar

 
Old October 26th, 2006, 04:57 AM
Registered User
 
Join Date: Oct 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
To include a js file into another js file jdang67 Javascript 4 February 28th, 2008 03:32 AM
Filter File Types in ASP.NET File Upload ramuis78 ASP.NET 2.0 Basics 2 May 31st, 2007 10:50 AM
asp file into a javascript file (js)? jstewie Javascript How-To 2 August 25th, 2005 08:32 AM
convert a js file from a asp file - change format jstewie Javascript How-To 0 July 21st, 2005 08:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.