The problem of the reference of JavaScript event response function

Source: Internet
Author: User

You often assign response functions in external JavaScript files, such as:

1 window.onload=function() {     2//...    3 }

Sometimes the response function has parameters:

<type= "button"  onclick= "Clickresp (' Hello World ')"  />

How is this scenario implemented in external JavaScript?

This involves two questions:
1. The problem of the transfer of anonymous functions. For example, var func=function() {}; in this case, the occurrence of the argument is not allowed at all, because it is defined rather than called.
2. How do I pass data from HTML to the linked external JavaScript file? For example, how does the "Hello World" in the example above be passed to JavaScript? This example can be solved with value= "Hello World" , but what if there are multiple parameters?

The first problem can be solved with a classic JavaScript feature-closures.
With var func=function() {}; for example, the first problem is that the argument is already in the domain or space of the func, even if the data needs to be obtained from HTML, This is just a matter of thinking about what happens after the data reaches the space that JavaScript handles. Therefore, this question is independent from the second question.
A closed packet is known to allow the inner layer function to share the domain of the outer function, so it can be determined that the closure resolves:

1 varArg_handler =function(ARG) {2 alert (ARG);3 };4 5 varreal_arg= "Hello World";6 varArg_wrapper=function(){7     returnArg_handler (real_arg);8 }9 TenWindow.onload=arg_wrapper;

The second problem can be solved with the HTML5 data-* attribute. As already mentioned, you can use the value to pass the parameter, now you can use the custom attribute to pass the parameter, there is no limit to the number of parameters:

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4     <Script>5 window.onload=function(){6             varFoo=document.getElementById ('Data'). getattribute ('Data-foo');7             varBar=document.getElementById ('Data'). getattribute ('Data-bar');8             varwrapper=function(){9 Alert (Handler (foo, bar));Ten             };  One             varHandler=function(x, y) { A                 returnx+y; -             };  - document.getElementById ('Data'). onclick=wrapper; the         };  -     </Script> - </Head> - <Body> +     <inputtype= "button"Data-foo= "Foo"Data-bar= "Bar"value= "click"ID= "Data"  /> - </Body> + </HTML>

When writing this sample code, a new question appears, which is recorded in another essay.

The problem of the reference of JavaScript event response function

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.