code master
code master

Reputation: 41

Adding a document.write in the load function of a new dashcode template causes rest of JS not to get generated

I started a new custom template for safari in dashcode ("This template creates a blank web application, ready for customizing."). It auto generates a function load in main.js that is called from the body in index.html:

function load() {
    dashcode.setupParts();
}

I added some JS code after the function which seems to execute as part of the HEAD when i run. I also added an onclick event in the body of index.html:

<body onload="load()";>
<input type="button" onclick="sayHello()" value="Say Hello" />
</body>

I also got the the button for it when I run.

Whenever I add a document.write call to the function load (which should just execute on load before the button is displayed) nothing else gets generated when I run. In other words whenever load becomes:

function load() {
    dashcode.setupParts();
    document.write("LOADING");
}

none of the javascript that i added after the function gets displayed. Also the onclick button that i added in the body doesn't appear.

Does anybody have an explanation for this behavior?

Upvotes: 1

Views: 89

Answers (1)

lucas
lucas

Reputation: 1505

calling document.write() after page load will overwrite the current page - javascript, html, everything - which is why it is so frowned upon generally. Look at DOM methods or innerHTML manipulation for alternatives.

Upvotes: 0

Related Questions