2012-02-18 03:17 阅读 61


I am thinking a way to add navigation bars to all of my webpages, and when I update the file, all pages should also update.
I researched on Google for a bit of time, what I found are:

  •  PHP 
  •  SSI  (I don't even konw what's this)
  •  JavaScript 

But which one is the best? And can someone explain what SSI is?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • 已采纳
    dongxiaoguang9108 dongxiaoguang9108 2012-02-18 03:49


    PHP would work fine, but using it would mean you'd have to set things up a bit differently, including doing things like:

    • Running PHP on your server
    • Renaming your .html files to .php
    • Changing your links to point to the .php files
    • Learning at least enough about PHP to use require


    SSI (Server Side Includes) are some ancient thing that nobody uses. Don't use them. But if you're curious, take a look at mod_include.


    Client-side javascript could sort of be made to work, but you're better off doing this kind of thing on the server.

    Here are some other options to consider.

    Another server-side language

    PHP isn't the only server-side language. You might prefer python, ruby, server-side javascript, etc.


    XSL transformations are well-suited to this kind of templating, and can be applied either by the browser or on the server. Support is good across browsers, and has been for some time. Extensions like "noscript" tend to break XSL transformations in some browsers, so applying them on the server is usually preferred when possible.

    iframe hack

    You could simply put your navigation in a separate HTML file and include it in an iframe on every page. Only use this as a last resort. If you cannot use a server-side language, cannot or do not want to use XSLT, and have relatively few pages, this might work okay. Otherwise, don't do it.

    点赞 评论 复制链接分享
  • douzepao0281 douzepao0281 2012-02-18 03:28

    SSI (Server-Side includes) are outdated. I would suggest not using them. JavaScript will work, but your browser would have to download an additional file just to construct the page. Also, people with JavaScript disabled would not be able to navigate your site. It really doesn't make sense to use JavaScript to construct your page, so don't use it unless you want special effects.

    PHP is the best option. You just have to include the navigation script at the top of your page like include_once('nav.php');.

    点赞 评论 复制链接分享
  • douqian4411 douqian4411 2012-02-18 03:29

    SSI stands for Server Side Includes which is the concept that means your files will all include the same file (for the navigation bar for example), so when you make any changes in that file, it'll get reflected in all your pages.

    PHP and javascript are two different things, PHP is a server-side language while javascript is a client-side language.

    If you just want a navigation bar, PHP will be enough. But if you want to add any client-side funcionalities like a popup menu under sections and things like that you'll have to use javascript too.

    点赞 评论 复制链接分享
  • duanpa2143 duanpa2143 2012-02-18 03:29

    But which one is the best It depends on what you are already using on your website.

    If it written on php then it is better to use php for the inclusion. By simple include('file_with_nav_bars_code'); at the right place in your template or code you are able to insert the static html code or the generated one by the file mentioned above.

    SSI (server-side includes) is good for the 'static' pages. It is like a simple scripting which can be done by the server after some setup. If you check templates of the error pages of the Apache server you will see SSI directives there, which include the footer, header, outputs some basic information like time, requested URL and so on. Inclusion of the some code will look like <!--#include virtual="/file_with_nav_bars_code" --> and in the same way you could include even some CGI script <!--#exec cgi="/cgi-bin/example.cgi" --> Read more here and here

    The javascript is also good for static pages. You have to load javascript code which will 'generate' the html code you need and output it at the place on the page you want. It has one serious negative effect - search engines will not be able to follow the links generated by js as they will not run it (as the visitors with js turned off, but I do not know such cases. Even Opera mini is able to render the results of the simple js output). The simple way is to include the js script at the place you want to have a nav bar: <script src='nav_bar.js'></script> and in nav_bar.js you will (as a very simple example) something like this: document.write('some html code of the bar here, with escaped new lines and quotes');

    ps: For many years of experience I've tried all of them :) I used js and SSI for the early static pages, now I'm only using php and other server-side scripting languages. You can still use javascript with php generated pages, but for the SSI you have to tell the webserver to pass the output of the php one more time looking for SSI directive instead of returning it back to the browser.

    点赞 评论 复制链接分享
  • dongyi8416 dongyi8416 2017-06-30 10:41

    SSI still works on modern servers using the v function or % (tested on Apache/2.4.18).

    Example (nav include):

    <!--#include file="nav.html" -->

    Example active menu item (if test on document name):

    <!--#if expr='v("DOCUMENT_NAME")=~/about.html/'-->
    <a class="active" href="#">
    <!--#else -->
    <a href="about.html">
    <!--#endif -->

    Example 2 (if test on document path):

    <!--#if expr="%{DOCUMENT_URI} =~ /product/"-->
    Product path
    Some other path

    .htaccess to get SSI working (example):

    AddType text/html .shtml
    AddHandler server-parsed .html
    AddHandler server-parsed .shtml
    Options Indexes FollowSymLinks Includes

    Older Apache version use different expr syntax.

    点赞 评论 复制链接分享