HTML编码和PHP或Javascript代码的混合 [英] HTML coding and mixing of PHP or Javascript code

查看:131
本文介绍了HTML编码和PHP或Javascript代码的混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分配了一个任务来修改网站,目前我正在处理index.html页面。以前的编码器在它们之间混合了大量的javascript和CSS代码,它变得很难阅读。



我想知道是否需要包含标签和CSS代码在中间? PHP代码怎么样?



如果有多个javascript和CSS文件

解决方案

将javascript保存在单独的文件中,保持css在一个单独的文件中,并让他们都从您的HTML引用。这些引用文件相对于HTML的顺序无关紧要。至于PHP,我不会太担心它与HTML混合(只保留你的函数,类和其他脚本在单独的文件中,并包括他们的PHP头)。



如果是每个页面上的同一个CSS,拥有一个外部文件缓存有助于节省带宽。如果有不同的规则与HTML混合不同的元素类型,你可能会有一些冲突,但如果你重写它,它会变得更干净,更容易维护。



我喜欢保留一个像这样的文件结构:

  index.php 
/ css
main.css
othercssfiles.css
/ javascript
main.js
otherjsfiles.js
/ template
header.php
页脚。 php
/ scripts
functions.php
otherscripts.php

然后在我的头文件中,我将放置HTML代码引用的css和javascript目录中的文件。在根目录下,我的index.php文件将include();



otherjsfiles.js和othercssfiles.css可用于单个页面可能有具体要求,需要大量的css和javascript,大多数其他页面不需要。这意味着其他页面不需要提取不必要的数据,它保持页面特定的代码独立于整个网站的代码。



我发现这是一个简单的方法来跟踪







已编辑:



如果要引用多个JavaScript和CSS文件
,单个或标签?


最好将它们合并到单个文件中,以节省HTTP请求

 < script type =text / javascriptsrc = /javascript/main.js\"> ;</script> 
< link rel =stylesheethref =/ css / main.css>






此外,您似乎可以使用a a CSS美化可读性, JavaScript美化可读性和 JavaScript minifier ,当你完成阅读它(保持可读版本),并希望节省带宽。这些工具在您开发维护未创建的网站时非常有用。


I am assigned a task to revise a website and at present I am working on index.html page. the previous coder has mixed a lot of javascript and CSS code in between and it is becoming difficult to read.

I want to know whether it is necessary to include tags and CSS code in between? What about PHP code? Where each must reside?

Edited:

If multiple javascript and CSS files are to be referenced, how to include in a single or tag?

解决方案

Keep your javascript inside a separate file, keep your css inside a separate file and have them both referenced from within your HTML. The order of these referenced files relative to the HTML does not matter. As for the PHP, I wouldn't worry too much about it being mixed in with the HTML (just keep your functions, classes and other scripts in separate files and include them with PHP in the header).

If is the same CSS on each page, having an external file that caches helps to save bandwidth. If there are different rules intermixed with the HTML for different element types you may have some conflicts, but if you rewrite it, it will end up being a lot cleaner and easier to maintain later.

I like to keep a file structure like so:

index.php
/css
   main.css
   othercssfiles.css
/javascript
   main.js
   otherjsfiles.js
/template
   header.php
   footer.php
/scripts
   functions.php
   otherscripts.php

Then in my header file I would place HTML code referencing the files in the css and javascript directories. And in the root directory my index.php file would include(); the header at the top and the footer at the bottom.

"otherjsfiles.js" and "othercssfiles.css" can be used in cases where a single page may have a specific requirement, requiring a lot of css and javascript that most other pages don't need. It means other pages do not need to fetch unnecessary data and it keeps page specific code separate from the entire sites code.

I have found this an easy way to keep track of various aspects of the code that makes up a HTML page, but naturally you will find ways to organize it that makes sense to you.


Edited:

If multiple javascript and CSS files are to be referenced, how to include in a single or tag?

It would be better to combine them into a single file to conserve HTTP requests (which take time). Then you would just include those css and javascript files like normal.

<script type="text/javascript" src="/javascript/main.js"></script>
<link rel="stylesheet" href="/css/main.css">


Additionally it seems like you could use the services of a CSS beautifier for readability, a JavaScript beautifier for readability and a JavaScript minifier for when you are done reading it (keep the readable version) and want to save bandwidth. These tools are especially helpful when you are working on maintaining a website you did not create.

这篇关于HTML编码和PHP或Javascript代码的混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆