The examples that I am going to show you here, covers three very popular Asp.Net code behind procedures to write Client Scripts.
01) RegisterClientScriptBlock – Register a block of Script without the “<script>” tags.
02) IsStartupScriptRegistered – Returns a Boolean (true or false), when a particular start up script is registered.
03) RegisterStartupScript – Register the Client Script from Code Behind.
Note: Please do not skip the conclusion part of this article, at the end. It has an important message, related to this article.
In addition, I have added a Button control in the body section. The button’s click event will call another function called startup(). Look carefully inside the above markup and script. We have not declared the startup() function anywhere yet. In fact, we will register the function using code behind procedure.
Typically, a browser will stop rendering (loading) the page (for a while) until it read and loads the script completely. This will effect the page load time, which may sometimes be very annoying for users, especially if they are using a mobile phone. Therefore, it is advisable to write the script after the <body> tag.
If you have written the scripts in a .js file and you add that file using the scripts’ src attribute in your web page, then you must use the HTML async attribute. This attribute will ensure that the file is loaded asynchronously.