dkris
dkris

Reputation: 1280

Scope of This JavaScript Variable

I have a question and an issue wrt the code below:

My question is what is the scope of the variable loaded here. The reason why I ask this is the onload="if(loaded==1)inittextarea() code is working fine on Firefox and not IE8. Why is this happening? Is there something specific I need to do here? Or is it not a valid practice?

<html>
<head>
<title>Some Page</title>
<link rel="stylesheet" href="../css/default.css" type="text/css">
<script type="text/javascript"> 
    var loaded = 0; /*Point of interest*/
    function jsLoaded() {
    loaded =1; 
}
</script>
<script type="text/javascript">
    function inittextarea() {
        alert("test")
        tinyMCE.init({  
            elements : "content",
            theme : "advanced",
            readonly : true,
            mode : "exact",
            theme : "advanced",
            readonly : true,
            setup : function(ed) {
                ed.onInit.add(function() {
                tinyMCE.activeEditor.execCommand("mceToggleVisualAid");
                });
            }
        });
    }
</script>
<script src="../js/tiny_mce/tiny_mce.js" onload="jsLoaded()" type="text/javascript"></script>
</head>
<body onload="if(loaded==1)inittextarea()"><!--Works on Firefox only-->
    *Usual stuff*
</body></html>

Any pointers please?

Upvotes: 0

Views: 217

Answers (3)

Mongus Pong
Mongus Pong

Reputation: 11477

The problem is that Script onLoad doesn't work in IE.

Upvotes: 1

Tim Down
Tim Down

Reputation: 324507

onload on <script> elements isn't generally supported. You could work around this by simply putting a <script> element below the TinyMCE <script> element:

<script src="../js/tiny_mce/tiny_mce.js" type="text/javascript"></script>
<script type="text/javascript">
    jsLoaded();
</script>

Although actually this is all unnecessary: by the time <body>'s onload fires, all the JavaScript will be guaranteed to have loaded, so there's no need for the check:

<body onload="inittextarea()">

Upvotes: 3

starskythehutch
starskythehutch

Reputation: 3338

The use of onload in the body tag is frowned upon now as best practice. Perhaps you should revise they way your initialisation happens.

This article explains.

Upvotes: 2

Related Questions