I have this script in the separate Sample.js file:
function MyPrint(text)
{
document.write(text);
}
And I have the following HTML page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" ".dtd">
<html xmlns="">
<head>
<title>Silly example</title>
</head>
<body>
<div>
<script type="text/javascript" src="JavaScript/Sample.js">
MyPrint("Hello silly world!");
</script>
</div>
</body>
</html>
Final result is that the text "Hello silly world!" is NOT printed on the page. What should I do to make this work? I would prefer not to move the script tag to the head, if possible. Thanks.
I have this script in the separate Sample.js file:
function MyPrint(text)
{
document.write(text);
}
And I have the following HTML page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head>
<title>Silly example</title>
</head>
<body>
<div>
<script type="text/javascript" src="JavaScript/Sample.js">
MyPrint("Hello silly world!");
</script>
</div>
</body>
</html>
Final result is that the text "Hello silly world!" is NOT printed on the page. What should I do to make this work? I would prefer not to move the script tag to the head, if possible. Thanks.
Share Improve this question asked Oct 24, 2010 at 20:47 BorisBoris 10.3k35 gold badges113 silver badges149 bronze badges 2- In which browser? That's the problem with this 1990's legacy scripting stuff. – Manius Commented Oct 24, 2010 at 20:50
- 3 remove the src from the script tag and put in a new one. – Shawn Mclean Commented Oct 24, 2010 at 20:50
4 Answers
Reset to default 18I think the src tag overrides whatever's inside.
Try the following:
<script type="text/javascript" src="JavaScript/Sample.js"></script>
<script type="text/javascript">
MyPrint("Hello silly world!");
</script>
Your script can be considered loaded after the closing </script>
element. This should work:
<!-- just a sidenote: type="text/javascript" is the default for script as of html5 -->
<script src="JavaScript/Sample.js"></script>
<script>MyPrint("Hello silly world!");</script>
<script type="text/javascript">
MyPrint("Hello silly world!");
</script>
window.onload = function(){
MyPrint("Hello silly world!");
};