New Function in Javascript

Source: Internet
Author: User

Today, I have nothing to worry about. I wrote an IFRAME adaptive height test and failed to bind the IFRAME onload event in the script.

 

Function suitself2 (myifrobj) {<br/> myifrobj = NULL? Document. getelementbyid ('myifr _ 2'): myifrobj; </P> <p> If (myifrobj = NULL) Alert ('suitself2 myifrobj is null '); </P> <p> var myifrbody = myifrobj.content?#doc ument. body; </P> <p> If (myifrbody = NULL) {// If the IFRAME page is not correctly loaded, the body cannot be obtained, set a height by yourself <br/> alert ('suitself2 myifrbody is null'); <br/> myifrobj. style. height = 600; <br/>}</P> <p> If (myifrobj. contentWindow. attachevent) {<br/> alert ('assad1'); <br/> myifrobj. contentWindow. attachevent ("onLoad", function () {// ie <br/> suitself (myifrobj); <br/> alert ('attachevent'); <br/> }); </P> <p> myifrobj. contentWindow. attachevent ("onscroll", function () {<br/> suitself (myifrobj); <br/> alert ('onscroll '); <br/> }); </P> <p >}< br/> else if (myifrobj. contentWindow. addeventlistener) {// Mozilla, Firefox <br/> alert ('assad2'); <br/> myifrobj. contentWindow. addeventlistener ("LOAD", function () {<br/> suitself (myifrobj); <br/> alert ('addeventlistener '); <br/>}, false ); <br/>}< br/> else {<br/> alert ('assad3'); <br/> var oldonload = myifrobj. contentWindow. onload; <br/> myifrobj. contentWindow. onload = new function () {<br/> If (typeof oldonload = 'function') {<br/> oldonload (); <br/>}< br/> suitself (myifrobj); <br/> alert ('onload'); <br/> }; <br/>}</P> <p >}< br/>

 

 

Put myifrobj. contentWindow. attachevent ("onLoad", function () {// IE
Suitself (myifrobj );
Alert ('tachevent ');
});

 

Change to myifrobj. contentWindow. attachevent ("onLoad", new function () {// IE
Suitself (myifrobj );
Alert ('tachevent ');
});

Pay attention to the red part. I found that the height of IFRAME is adaptive.

 

After checking function () {} and new function () {}, we found that the former typeof is function, and the latter is object.

 

When an onscroll event is added later, alert ('onscroll ') is executed in the pop-up page. At this time, I felt a bit wrong,

 

Add the script new function () {alert ('1232asdasd') ;}, as expected, alert ('1232asdasd'); executed, and executed before the page is displayed.

 

From this result, function () {} declares and defines a method. New Function () {} is a declaration, defines a method object, and executes this method object, this method object cannot recognize the following elements.

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.