简体   繁体   中英

javascript won't execute - only displays code

I'm trying to execute a javascript file in my browser but the code is displayed and not actually executed. I'm using firefox and I made sure javascript is enabled. I tried using a .js extension and .shtml and both just display the code. The file is located in my apache htdocs folder and it's version 2.2.

I'm trying to run this hello world code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript" src="helloworld.js"></script>
</head>
<body>
<p id="hello"></p>
</body>
</html>

Here's the javascript

document.getElementById('hello').innerHTML = 'hello world';

Any suggestions?

You have no element with the ID of ex .

With the HTML you've shown, your line should be:

document.getElementById('hello').innerHTML = 'hello world';

See now that <p> tag has the id="hello" -- how would a call to getElementById('ex') be able to find that?


EDIT

Working JSFiddle example

try something like this

        <html>
        <head>
        <script type="text/javascript" src="helloworld.js"></script>
        </head>
        <body  onload="onload1()">
        <p id="hello"></p>
        </body>
        </html>

helloworld.js

        function onload1(){
         document.getElementById('hello').innerHTML = 'hello world';
        }

Save the above code as .htm or .html. Your server is probably not configured to use SSI (.shtml extension).

You said you saved it as .js and as .shtml, this is probably where you have gone wrong. .js files will not display properly when opened directly by the browser, as the browser is not intended for displaying JavaScipt, thus you must use a file type that is intended for HTML. The .shtml extension is for HTML, however it is only used with servers using SSI (Server Side Includes), so if the server does not use SSI it will not work.

Usually you will see code when the mime type is wrong. The mime type is how the server tells the browser what kind of file it is sending http://en.wikipedia.org/wiki/Internet_media_type . The server sends the mime type, which it determines by looking a the files extension, or in some cases certain parts of the file (and server side code can adjust the mime type as well). The server will give files with the .htm and .html extension the HTML mime type, but .js will not have that type. The .shtml file will only be given the HTML mime type if the server uses SSI. So when the browser gets those files it will intemperate them as plain text. This is why you see the code.

So either change the extension to .htm or .html, or enable SSI on the server (or ensure it is configured to work in that directory).

As a side note, your JS will not do anything as there is no element with the ID 'ex', however that should not produce the problem of seeing code. It will however give you trouble down the line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM