开发者

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 : "co开发者_JAVA技巧ntent",
            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?


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()">


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜