And the fol...">

Window object acting weird in Chrome and IE

Consider the following HTML example:

<div id="about">
 <!-- content here -->
</div>

And the following script

var about = (function ($, window, document) {
    "use strict";

     var methods;

     methods = {
        init: function () {
        // Do things here 
        }
     };

     return methods;
} (jQuery, window, document));

At this point, the variable aboutshould be bound to the object window.

In Firefox (3.6.17) I can write

window["about"] 

And if about is not processed yet, it will return undefined, if it is, it will return the object as I expect.

However, the problem is that the same code window["about"]in Chrome and IE (7 and 8) returns the actual HTML object. In the above example, it will return the following:

    <div id="about">
     <!-- content here -->
    </div>

Why is this happening?

Also, is there a better way to check and see if an object is accessible aboutthan using an element window? I understand that ideally I do not want to put a window object, but this is a completely different issue.

thank

+3
source
4

WebKit, , IE id window.

.

+4

Chrome HTML . , , , , ?

http://jsfiddle.net/robert/Hnw7y/

+1

One way to distinguish between HTMLElementfunction and function is to check the type of object:

if (typeof window.about === 'function') {
    // the 'about' function has been defined
}
+1
source

Unfortunately, the only way to do this is:

function global(name) { return eval(name); }

if( global("about") )
   ...

Try the following: http://jsfiddle.net/PMqPv/1/

0
source

All Articles