views:

120

answers:

6

I have the following HTML and Javascript in a page that should display "TEXT" in the body and alert me with "alert!" However, it only displays "TEXT"; my Javascript console says "msg is undefined."

<html>
<head>

<script type="test/javascript">
function msg() {
    alert("alert!");
}
</script>

</head>
<body>

<p>
    <script type="text/javascript">
        document.write('TEXT');
        msg();
    </script>
</p>

</body>
</html>
+9  A: 

If that's a direct copy/paste, then your problem appears to be a type:

 <script type="test/javascript">

You have test there instead of text

RHSeeger
UGH. I swear I started at that line 10 times and didn't see it.
Andrew Keeton
*stared - It's been a long day...
Andrew Keeton
The irony of a typo when commemting about not seeing a typo is not lost on me (I misspelled commenting to make you feel better ;)
RHSeeger
+1  A: 

You typed test/javascript That could be the problem :)

micmoo
+1  A: 

you spelled 'text' wrong in the opening script tag:

GSto
+1  A: 
<script type="test/javascript">

You have a typo here. It should say "te**x**t/javascript". It is not parsing correctly in the current state.

Josh Lindsey
A: 

you have a typo:

<script type="test/javascript">

should be

<script type="text/javascript">
pixeline
+1  A: 

As so many others have pointed out, the type attribute of the script tag is wrong; it should be "text/javascript". To avoid this in the future, you can leave off the type attribute altogether as browsers will default to "text/javascript" unless otherwise specified.

geowa4