Javascript references in asp.net (direct and indirect introduction)

Source: Internet
Author: User

There are many methods to introduce Javascript in Asp.net. I mainly talked about the following two types during my career as a cow.
In my opinion, it can be divided into direct or indirect introduction.

1. Direct Introduction. Call custom javascript Functions on the front-end page:
1. Open the front-end page and add the script element between the head element and set the type element to "text/javascript ". Then write the javascript UDF.Copy codeThe Code is as follows: <Script type = "text/javascript">
Function ShowName (str)
{
Alert ("News Type: (" + str + ")");
}
</Script>
<Title> using javascript </title>
</Head>

2. Access between body elements through events. For example, the following example shows how to access the ShwoName () of javascript Functions through the onclientclick event of button1:Copy codeThe Code is as follows: <SPAN style = "FONT-SIZE: 18px"> <asp: button ID = "Button1" runat = "server" Text = "Button" onclientclick = "ShowName ('Chinese news ')"/> </SPAN>

When you click a button to run the project, "news category: Chinese News" is displayed. This means that javascript Functions are defined and called on the front-end page.

Ii. Indirect Introduction. In the foreground, call the corresponding functions by introducing the js file.
The steps are the same as those for direct access. They must be introduced first and called. Because direct introduction writes javascript Functions on the front-end interface, and indirect references are written in a JS file, indirect references must specify the js file to be called.

The Code is as follows: The red part indicates the javascript file.Copy codeThe Code is as follows: <Script type = "text/javascript" <SPAN style = "COLOR: # ff0000"> src = "ChanggeType. js"> </SPAN>
</Script>
<Title> using javascript </title>
</Head>

After the definition is completed, call it in the body.

PS: both methods can be introduced. Direct introduction may be relatively simple and intuitive to start writing. However, javascript code and HTML tags are directly stored on the front-end page together with multiple codes. As a result, the page becomes bloated and will be difficult to modify in the future, poor flexibility. Therefore, direct introduction of this method is suitable for small projects, maintenance, and modification.

We recommend that you put javascript code into an independent js file during programming. Other pages use the corresponding javascript code by introducing the js file, which is flexible in operations, maintenance, and easy to modify.

Here we talk about the two ASP.net methods for Introducing javascript from the foreground, and there are also many methods for accessing javascript from the background, which are not involved yet.

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.