Forum Moderators: open

Message Too Old, No Replies

js inside another js file

         

ddsuresh

5:26 am on Dec 20, 2006 (gmt 0)

10+ Year Member



How I expected means both Hello has to be under one table.
But see this example code, so anybody help me with some solutions.

After creating the table, then only <script> tag is working inside this code.
That's way second "hello" which is inside the external javascript, getting outside of the table. It happens only in IE browsers.

How do I make it second "Hello" inside of the table.

// test.htm

<html>
<head>
<title>Untitled Page</title>
</head>
<body>

<script language="javascript" type="text/javascript" src="test1.js"></script>

</body>
</html>

// test1.js

document.write('<table cellpadding="0" cellspacing="0" border="1" bordercolor="red">');
document.write(' <tr>');
document.write(' <td>');
document.write(' Hello');
document.write(' </td>');
document.write(' <td>');
document.write(' <script type="text/javascript" src="test2.js"></script>');
document.write(' </td>');
document.write(' </tr>');
document.write('</table>');
document.write('<br><br>');

//test2.js
document.write('Hello');

Fotiman

3:32 pm on Dec 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To quote [javascript.crockford.com] Douglas Crockford [crockford.com]:

The document.write method provides a way of incorporating strings into the HTML content of the page. There are better ways to do that, such as .innerHTML and .createElement or HTML cloning patterns. Use of document.write should be avoided.

document.write is recklessly dependent on timing. If document.write is called before the onload event, it appends or inserts text into the page. If it is called after onload, it completely replaces the page, destroying what came before.

document.write encourages bad structure, in which script and markup are intermingled. A cleaner structure has minimal interaction between markup and script.

Also, you should not be relying on JavaScript to write your content.

[edited by: Fotiman at 3:34 pm (utc) on Dec. 20, 2006]

rocknbil

7:43 pm on Dec 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is actually a more important consideration, but to directly answer the question, you need to fool the browser somehow, like this:

var msg =' <scr' + 'ipt type="text\/ja' + 'vas' + 'cript" src="test2.js"><\/' + 'sc' + 'ript>';
document.write(msg);