JavaScript references in asp.net (direct introduction and indirect Introduction) _ Practical Tips

Source: Internet
Author: User
Tags html tags
There are a number of ways to introduce JavaScript in asp.net. In the beef brisket when the main talk about two kinds.
Personal opinion can be divided into direct introduction, and indirect introduction.

first, Direct introduction。 Invoke a custom JavaScript function on the foreground page:
1. Open the foreground page to add a SCRIPT element between the head elements and set the type element to "Text/javascript". Then write JavaScript-customized functions.
Copy Code code as follows:

<script type= "Text/javascript" >
function ShowName (str)
{
Alert ("News Category: (" +str+ ")");
}
</script>
<title>using javascript</title>

2, the body elements, through the event to access, such as through the Button1 Click event (OnClientClick) to access the JavaScript function of the shwoname () examples are as follows:
Copy Code code as follows:

<span style= "font-size:18px" ><asp:button id= "Button1" runat= "Server" text= "button" onclientclick= "ShowName (' China News ') "/></span>

When you run the project, when you click the button, the News category: China News, which is the direct definition and invocation of JavaScript functions on the front page.

second, indirect introduction。 In the foreground by introducing a JS file to call the corresponding function.
The same as the direct access step, are required to be introduced first in the call. Because the direct introduction is the JavaScript function written in the foreground interface, the indirect reference is written in a JS file, so the indirect reference needs to indicate the JS file to call.

The code is as follows: The red section indicates the JavaScript file.
Copy Code code as follows:

<script type= "Text/javascript" <span style= "COLOR: #ff0000" >src= "Changgetype.js" ></SPAN>
</script>
<title>using javascript</title>

Once the definition is finished, it is called in the body.

PS: Both methods can be introduced. Direct introduction may start to write more simple, intuitive, but the JavaScript code and HTML tags directly in front of the page, the code a lot, the page becomes bloated, and later to change also trouble, flexibility is not good. Therefore, the direct introduction of this method is suitable for small projects, maintenance, and modification is not large.

It is recommended that you do programming, bar JavaScript code into a separate JS file, other pages by introducing the JS file to use the corresponding JavaScript code, flexible operation, maintenance, changes are convenient.

Here are two ways to introduce JavaScript from the foreground, and there are a lot of ways to access JavaScript from the background, not yet asp.net.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.