doucang9673 2010-01-15 11:15
浏览 43
已采纳

HTML编码和PHP或Javascript代码的混合

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?

  • 写回答

6条回答 默认 最新

  • dswu26846 2010-01-15 11:16
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!