An analysis of the problem of the direct quantity of a function in Javascript-mozilla and IE _javascript skills

Source: Internet
Author: User
I write this here is also a point in doing JS when the problem encountered, when a note. Did not expect to be Hax "batch" of a meal, but also said that there is some truth. After all, this content is also perfunctory

In general, we use the direct amount of the function in JS to name a simple function.


<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 definition of the direct quantity of the function).

But there is another simpler way of naming 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>

And this does not report syntax errors in Mozilla, but throws an exception that operators are illegal:
The following code captures the exception thrown by Mozilla and prints it out

<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>

The exception contents are:
[Exception ... "Illegal operation on Wrappednative prototype object" Nsresult: "0x8057000c (Ns_error_xpc_bad_op_on_wn_proto)" Location : "JS Frame:: file:///C:/Documents%20and%20Settings/Administrator/Local%20Settings/Temp/non4A.htm:::: Line" Data: No

It's not yet known if you can set it up in Mozilla Config anyway, so be careful when writing code.

I search Google for getElementById 0x8057000c keywords, or there are a lot of similar cases, such as

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

There is no better solution, but can be adapted, than to write

<div id= "foo" ></div>
<script>
document.$ = document.getElementById;
Alert (document.$ ("foo"). InnerHTML);
</script>

Because JS can dynamically add properties and methods to objects, the above example can be tested in IE and Moz.

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.