简体   繁体   English

外部文件中的Javascript

[英]Javascript in an external file

Before I ask this question I must point out that I have tried to search for EVERYTHING! 在我提出这个问题之前,我必须指出我曾试图寻找一切!

My question is how can I run javascript from an external file instead of inside my php / html. 我的问题是如何从外部文件而不是在我的php / html中运行javascript。 What I'm trying to do is. 我想做的是。

    function ClearForm() {
      document.form.message.value="";
    }

    function comeBack(){
      if (document.form.message.value == "") {
         document.form.message.value="What's on your mind?";
      }      
    }

I have included <script type="text/javascript" src="javascript.js"></script> in the <head> and I have a file in the root called javascript.js and my php file is in the root too so that shouldn't be the problem! 我在<head>包含<script type="text/javascript" src="javascript.js"></script> <head> ,我在根目录中有一个名为javascript.js的文件,我的php文件也在根目录中所以这应该不是问题! But how do I run that pieces of code you see above in the javascript.js file instead of in my php file. 但是如何在javascript.js文件而不是在我的php文件中运行上面看到的那些代码片段。 It work's fine if I have it in the php file but I want to separate things! 如果我在php文件中有它,它工作正常,但我想分开的东西!

I have also tried to give the form / input field an id and then use getElementById in the external JavaScript file. 我还试图给表单/输入字段一个id,然后在外部JavaScript文件中使用getElementById。

But as you can see and hear I'm kinda new to JavaScript so I'm apparently doing something wrong here. 但是你可以看到和听到我对JavaScript有点新意,所以我显然在这里做错了。

If the above code is the only thing in your Javascript.js file, then you need to call the functions to run the code. 如果上面的代码是Javascript.js文件中的唯一内容,那么您需要调用函数来运行代码。

You've included the external Javascript file correctly - however, because all of your JS is included within functions, these function/s must be called before the code will run. 您已正确包含外部Javascript文件 - 但是,因为所有JS都包含在函数中,所以必须在代码运行之前调用这些函数。

A call to 'ClearForm()' or 'comeBack()' from within your PHP file should run the code. 从PHP文件中调用'ClearForm()'或'comeBack()'应运行代码。

That JS file will have to be in the same folder as your PHP page. 该JS文件必须与PHP页面位于同一文件夹中。

Test whether the file is found or not by adding this line at the top of the js file 通过在js文件的顶部添加此行来测试是否找到文件

alert('js file found OK!');

document.form is an array, so if you only have one form use: document.form是一个数组,所以如果你只有一个表单使用:

document.forms[0]

Also depending on which browser you use, find and install some Developer Tools to help you identify these errors. 此外,根据您使用的浏览器,查找并安装一些开发人员工具,以帮助您识别这些错误。

You have declared those functions in the <head>. 您已在<head>中声明了这些函数。 All fine. 一切都很好。

The question is when do you want to call/run those functions? 问题是你想什么时候 打电话/运行这些功能?

If you simply want to run them at the end of the page, then you can add another external javascript file and include it using <script src="my_external_file.js"> right before the </body> tag. 如果您只是想在页面末尾运行它们,那么您可以添加另一个外部javascript文件,并在</ body>标记之前使用<script src =“my_external_file.js”>包含它。

Otherwise, you have to declare onXXX handlers, like onLoad() for the document, onClick() for certain elements, onSubmit() for forms, etc. These, too, can be declared in an external file, but specified after the relevant elements are loaded. 否则,您必须声明onXXX处理程序,例如onLoad()用于文档, onClick()用于某些元素, onSubmit()用于表单等。这些也可以在外部文件中声明,但在相关元素之后指定已加载。

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

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