简体   繁体   English

JavaScript无法执行-仅显示代码

[英]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. 我正在尝试在浏览器中执行一个javascript文件,但是该代码已显示并且并未实际执行。 I'm using firefox and I made sure javascript is enabled. 我正在使用firefox,并确保已启用javascript。 I tried using a .js extension and .shtml and both just display the code. 我尝试使用.js扩展名和.shtml,两者都只显示代码。 The file is located in my apache htdocs folder and it's version 2.2. 该文件位于我的apache htdocs文件夹中,它的版本为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 这是JavaScript

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

Any suggestions? 有什么建议么?

You have no element with the ID of ex . 您没有ID为ex元素。

With the HTML you've shown, your line should be: 使用显示的HTML,您的行应为:

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? 现在看到<p>标记具有id="hello" -调用getElementById('ex')怎么能找到它?


EDIT 编辑

Working JSFiddle example 工作JSFiddle示例

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 helloworld.js

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

Save the above code as .htm or .html. 将上面的代码另存为.htm或.html。 Your server is probably not configured to use SSI (.shtml extension). 您的服务器可能未配置为使用SSI(扩展名为.shtml)。

You said you saved it as .js and as .shtml, this is probably where you have gone wrong. 您说过将其另存为.js和.shtml,这可能是您出错的地方。 .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. 直接由浏览器打开时,.js文件将无法正确显示,因为该浏览器不适合显示JavaScipt,因此您必须使用适合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. .shtml扩展名是针对HTML的,但是仅与使用SSI(服务器端包含)的服务器一起使用,因此,如果服务器不使用SSI,它将无法工作。

Usually you will see code when the mime type is wrong. 通常,当mime类型错误时,您会看到代码。 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 . MIME类型是服务器如何告知浏览器它正在发送哪种文件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). 服务器发送mime类型,它是通过查找文件扩展名确定的,或者在某些情况下会发送文件的某些部分(服务器端代码也可以调整mime类型)。 The server will give files with the .htm and .html extension the HTML mime type, but .js will not have that type. 服务器将为扩展名为.htm和.html的文件提供HTML mime类型,但.js不会具有该类型。 The .shtml file will only be given the HTML mime type if the server uses SSI. 如果服务器使用SSI,则.shtml文件将仅具有HTML mime类型。 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). 因此,可以将扩展名更改为.htm或.html,或者在服务器上启用SSI(或确保将其配置为在该目录中工作)。

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. 附带说明一下,您的JS不会做任何事情,因为没有ID为'ex'的元素,但是这不会产生看到代码的问题。 It will however give you trouble down the line. 但是,它将给您带来麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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