mike
mike

Reputation: 1137

ie 6 cant find javascript definitions

i have a javascript file a.js ,it content is sometime like this

window.model={};

model.init=(
function(){return "something"}
)();

in my html files , i have something like this:

<script type="text/javascript" src="path/to/a.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
       model.init();
    });
</script>

in chrome , firefox and ie8 it works . but in ie6 or ie7 it will so model is not defined .

i don't know why . could anyone help me .

thanks !


i put some alert

in html :

<script type="text/javascript" src="path/to/a.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
           alert("ie6");
       model.init();
    });
</script>

in js

alert("ie7");
window.model={};

model.init=(
function(){return "something"}
)();

it print "ie6" first , but "ie7" never print out

Upvotes: 3

Views: 104

Answers (2)

jrummell
jrummell

Reputation: 43087

Reference model the same way you declare it, as a property on window.

window.model.init=(function(){return "something"})();

$(document).ready(function() {
   window.model.init();
});

Or, you could do the opposite and declare it as a global variable.

var model={};

Upvotes: 2

Tei
Tei

Reputation: 1416

This can be caused by different execution order, or some "hoisting" problem.

Try initializing model with "var model = {}". Then try to dump stuff to a log to check the execution order. IE have tools to use console.log, or you can try firebug lite.

Upvotes: 1

Related Questions