0

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?

3 Answers 3

3

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()">
Sign up to request clarification or add additional context in comments.

1 Comment

Best answer. Why use jsLoaded when you're already handling the load event?
2

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.

2 Comments

Actually, using the onload attribute of the <body> element is the only standardised way of capturing the fact that the document has loaded so could be seen as best practice.
@tarskythehutch @Tim As of now i have a few limitations in using the approach mentioned in onlinetools.org/articles/unobtrusivejavascript/chapter4.html . So, is there a way i could mod something in the current approach to make this work in IE?
1

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

2 Comments

What do you think is the alternative for this? Any workarounds?
I always use JQuery. Then I initialize TinyMCE in the document.load. Works well and you get a lot extra with the library that will be useful! And you don't need to worry about individual browser differences as much. Everyones a winner! ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.