About JSP in <body onload= "fun ()" >body tag in onload function does not perform the problem

Source: Internet
Author: User

Problem Description:

In one page, we often initialize the data and initialize the data in the specified DOM element, and we use <body onload= "fun ()" > to load our data. OnLoad refers to the fact that all DOM elements are loaded before the JS function is executed, which provides a great help for initializing dynamic loading data. But sometimes we find that the fun () function in <body onload= "fun ()" > is sometimes not executed. The following is an analysis of the reasons and workarounds that OnLoad does not perform.

Problem Solutions:

1. An error occurred while the page was loading.

This is a code error, which could be a bug in loading the DOM element, resulting in the inability to execute onload. This is the time to process the code and see where the code went wrong.

2. The page has been loaded

The browser is loading the data in the lower right corner of the browser, there is time, because of the network (slow loading) or cannot find the resources, then the page will be loaded uniformly, the onload may not be able to execute. Because onload is loaded with all DOM elements to execute. This is also the process of code, find the resources can not be loaded, delete or find other ways to load.

3.<body onload= "Fun ()" >body This tag is overwritten, destroyed (emphasis)

When writing JSP pages, we often use a <jsp:include page= "body.jsp" ></jsp:include> to include a new page. But at this time, body.jsp will not have <body> tags, will be automatically destroyed. Let's give an example.

This is main.jsp source code

1 <%@ Page Language="Java"Import="java.util.*"pageencoding="gb2312"%>2 <HTML>3 <Head>4 <title>OnLoad does not load</title>5 </Head>6 <Body>7 <P>This is the main interface8 <Jsp:includepage= "body.jsp"></Jsp:include>9 </Body>    Ten </HTML>

This is body.jsp source code

1 <%@ Page Language="Java"Import="java.util.*"pageencoding="gb2312"%>2 <HTML>3 <Head>4 <title>Body page</title>5 </Head>6 <Body>7 <P>This is the Body page.8 </Body>    9 </HTML>

Then look at the effect after running, open the F12 development tool

We only see the <p> tag, stating that the body.jsp <body> tag is automatically destroyed.

If you find a problem, you need a solution. In fact, we found that onload is actually the windon.onload of another way of expression, the principle is the same, this time using Windon to implement loading data.

1 <scripy type= "Text/javascrip" >2function Fun () {3     alert (" Hello world! " ); 4 }56 window.onload=function() {7fun    (); 8 }9 </script>

This performs the fun () function after loading the page, and the effect is the same as <body onload= "fun ()" >.

About JSP in <body onload= "fun ()" >body tag in onload function does not perform the problem

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.