A direct amount of functions in Javascript-Mozilla and IE

Source: Internet
Author: User

In general, we use functions to name a simple function directly in JS.

<Script type = "text/javascript">
Var func = function (s) {window. alert (s )};
Func ("never-online ")
</Script>

This is also legal in IE or Mozilla (The ECMA standard has a direct function definition ).

However, there is another simpler naming method in IE to simplify our code:

<Div id = "demo"> http://blog.never-online.net </div>
<Script type = "text/javascript">
Var $ = document. getElementById;
Alert ($ ("demo"). innerHTML );
</Script>

In Mozilla, a syntax error is not reported, but an exception is thrown. The operator is invalid:
The following code captures and prints the exceptions thrown by Mozilla.

<Div id = "demo"> http://blog.never-online.net </div>
<Script type = "text/javascript">
Var $ = document. getElementById;
Try {
Alert ($ ("demo"). innerHTML );
} Catch (ex) {document. write (ex )}
</Script>

Exception content:
[Exception... "Illegal operation on WrappedNative prototype object" nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS frame: file: // C: /Documents % 20and % 20 Settings/Administrator/Local % 20 Settings/Temp/non4A.htm ::: line 22 "data: no]

I still don't know whether it can be set in Mozilla config. Pay attention to it when writing code.

I search for the getElementById 0x8057000c keyword on google. There are still many similar cases, such

Http://groups.google.co.uk/group/comp.lang.javascript/browse_thread/thread/65a1a23f5dd7c9ad/d264d04d9d768b28? Tvc = 2 # d1_d04d9d768b28

There is no better solution, but it can be changed.

<Div id = "foo"> </div>
<Script>
Document. $ = document. getElementById;
Alert (document. $ ("foo"). innerHTML );
</Script>

Because js can dynamically add attributes and methods to the object, the above example can be passed in ie and moz tests.

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.