Client Side in ASP.NET: Common Javascript Functions
Why?
An over all look at all or most of the currently employed web development areas will rival an interesting fact: You can utilize and make benefit of many of these areas without full or deep knowledge of their underlying technologies!This fact is simply the motivation of writing such a tutorial. If it is possible to gain all these benefits without digging deep into the technology itself, then why not to do so? Why wait to have a complete mastering of a given technology and then start utilizing it?
In this tutorial we are going to present you a set of famous and frequently used client side scripts. You can simply copy any of these scripts into your web application without giving any attention to their underlying technologies. If, in any case, further customization or configuration is essential to utilize any of these scripts then we will include this information as well.
All these scripts are client side scripts and if you are not familiar with client side scripts or need to contrast it with the more famous server side scripts you can review Wikipedia in this regard. All you need to know here is that the client side scripts are just a part of your HTML file.
We will dig deep and come up with things that are commonly used and useful ..... Let's start then!
Get input / Use input
To get an input from your user and then to use this input elsewhere (Example: to display that input somewhere on your page) use the following script:Text before user input! < HR > <!--================================================--> < SCRIPT LANGUAGE =JAVASCRIPT> <!-- input=window.prompt( "Type the number of attachments here" , "5" ); document.write( "<H3>Number of files to be attached: " +input+ "</H3>" ) //--> </ SCRIPT > <!--================================================--> < HR > Text after user input! |
Figure 1 |
Figure 2 |
Back and forward navigation
Most if not all web browsers have Back and Forward buttons. The best describing use case of these buttons is when you have a single page with many links. You click one of these links and then click the Back button to return to the main index page and click another link and so on.To implement the same behavior using a similar two buttons but inside your web page, you can use the following script:
<! --================================================-- > < p align ="center"> < input type ="button" value ="Back" onclick ="history.back();"> < p align ="center"> < input type ="button" value ="Forward" onclick ="history.forward();"> <! --================================================-- > |
Although a somewhat similar behavior can be implemented using the standard non-scripting HTML anchor <a> tag, the fact of that not file names exist at all in the above script makes it a much more efficient alternative.
When you run the above script you will see the following page:
Figure 3 |
Display Message boxes and alerts
To display an alert to your user, use the alert JavaScript function call. For example:
<!--================================================--> < SCRIPT LANGUAGE =JAVASCRIPT> <!-- alert( 'This is an ALERT message to the user!' ); //--> </ SCRIPT > <!--================================================--> |
Figure 4 |
Visual feed back (Background color)
It's sometimes desirable to give you user a proper feedback after an action he takes (after a button click for example). On of the most notable feedbacks is the change of the entire page background. For example, you can change your entire page background to red when your user clicks a critical button that performs a critical function.You can get a list of the supported colors from the references section at the end of the tutorial. The script we are going to present now is a function that changes the background of the page and a set of buttons that call that same function but with a different color parameter every time. Here's the script:
< p align =center> Page Text </ p > < p align =center> Page Text </ p > < p align =center> Page Text </ p > <! --================================================-- > < SCRIPT LANGUAGE =JAVASCRIPT> <!-- function ChangeBackgroundColor(color) { document.bgColor=color } //--> </ SCRIPT > < input type ="button" value ="Change to Cyan" onclick ="ChangeBackgroundColor('Cyan')" ID ="b1" NAME ="b1"> < input type ="button" value ="Change to Fuchsia" onclick ="ChangeBackgroundColor('Fuchsia')" ID ="b2" NAME ="b2"> < input type ="button" value ="Change to LightSlateBlue" onclick ="ChangeBackgroundColor('LightSlateBlue')" ID ="b5" NAME ="b5"> <! --================================================-- > |
Figure 5 |
Figure 6 |
Automatic page redirection
Several times you visit a website and you see a message saying that this page has been moved and after few seconds you are automatically redirected to the new and correct location of the page. Another variation is to have a slideshow in which a set of pages are being displayed successively one after one with a duration of few seconds within every page. Several other scenarios can be implemented, so the actual applications are open to your imagination.The script itself is as simple as the following:
< H3 > You will be redirected to the page "s4.htm" after 3 seconds .... </ H3 > <! --================================================-- > < SCRIPT LANGUAGE =JAVASCRIPT> <!-- //---------- function redirectPage() { location = "s4.htm" } //---------- setTimeout("redirectPage()", 3000); //--> </ SCRIPT > <! --================================================-- > |
When this script runs, it displays the following page:
Figure 7 |
No comments:
Post a Comment