IE cannot execute the code of the released script

Source: Internet
Author: User

After the IFRAME is deleted from the Dom, ie9 + recycles the memory. Impact scope: Applicable to Internet Explorer 9 and later.

Scenario (rather hidden !!!) :

Define a global variable on the home page, and then let the operations in the IFRAME on the Child page update this value.

 

1 // Home Page 2 top. g_config ={}; // defines a global variable

 

1 // IFRAME page 2 3 // operation global variable 4 If ('undefined' = typeof (top. g_config ['url _ list']) {5 top. g_config ['url _ list'] = []; 6} 7 top. g_config ['url _ list']. push ('something'); 8 // after the operation is complete, disable this IFRAME

 

It seems that there is basically no problem, but the IFRAME page is contained in a pop-up window. After the operation, the IFRAME will be removed from the Dom. Have you noticed this line of code:

1 top.G_config[‘url_list‘] = [];

 

In IFRAME, an array is created for the global variable top. g_config, And the array is a complex type object in Js. Therefore, a global variable references an array created in IFRAME, Which is recycled when the IFRAME is removed from the Dom. When the global variable is operated in the pop-up window for the second time, an error is reported: the code of the released script cannot be executed.

How to avoid this problem (so easy !) :

1 // Home Page
2 top. g_config = {'url _ list': []}; // defines a global variable.

 

1 // IFRAME page 2 3 // operation global variable 4 top. g_config ['url _ list']. push ('something'); 5 6 // after the operation is complete, disable this IFRAME

 

Idea: Update global variables on the IFRAME page to avoid reference of values.

Conclusion: Do not use global variables when using JS encoding. Be cautious when you cannot avoid it. When updating global variables in IFRAME, do not reference the variables. This bug is very concealed.

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.