How to include one HTML file into another?

后端 未结 4 897
栀梦
栀梦 2020-12-10 16:59

I have tried every possible question here on stackoverflow but unable to resolve this issue ...

    
    

        
相关标签:
4条回答
  • 2020-12-10 17:25

    HTML is Static

    It is not possible to include a HTML page within another HTML page with static HTML because it is simply not supported by HTML. To make your HTML dynamic, you have 2 possibilities: use a client side script or use a server side technology.

    ...Unless you start using frames (dropped with the HTML5 standard) or iframes that are most likely not a solution because of the fact that it is treated as a completely different web page.

    Client Solution

    You could create a JavaScript file and write your HTML with the document.write function and then include the JavaScript file where ever you need it. Also, you could use some AJAX for this, but there are JavaScript libraries out there that could ease your burden such as the popular jQuery library.

    Yet, I would not suggest using a client solution because it is more trouble than it is worth...

    Server Solution

    There are many server side solutions out there: ASP, ASP.NET, ASP.NET MVC, Java, PHP, Ruby and the list goes on. What you need to understand is that a server a side technology could be described as a parser for a specific server side language to automate certain tedious tasks and to perform actions that would represent a security risk on the client.

    Of course you will need to have the means to host such a site and to configure the hosting environment. For example, you could host a PHP website with Apache and even have a developer hosting environment such as /MAMP/LAMP/WAMP.

    You can view an example of including a page in PHP in the online documentation.

    Personally, I would be more inclined to use a server side solution.

    0 讨论(0)
  • 2020-12-10 17:25

    Make your html file you want to include to php file. (only change the extension - to .php). If the files are in one directory, include code is:

    <?php include "nameoffile.php" ?>
    
    0 讨论(0)
  • 2020-12-10 17:26

    HTML doesn't have an 'include' mechanism - I'm not sure where you've seen these solutions on StackOverflow. You've probably been looking at answers for a server side language such as PHP or ASP.

    You do have the following options for 'pure' HTML where you only have static pages and JavaScript:

    1. Frames
    2. Ajax

    See my answer here for sample code and more details (and also a caveat about SEO if that matters to you).

    0 讨论(0)
  • 2020-12-10 17:38

    The former syntax is SSI, the latter is PHP. Both are server technologies and will only work if accessed from an HTTP server that supports and is configured to check the file for the syntax (which usually means you have to turn the support on and use a .shtml/.php file extension (or change the config from the default to determining which files to check)). Other server side technologies are available.

    The only "include" mechanisms in HTML itself are (i)frames and objects.

    You could also consider a template system such as TT that you could run as a build step to generate static HTML documents (NB: TT can also be used on the fly).

    0 讨论(0)
提交回复
热议问题