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.