开发者

Call one html file from another

I want to call one html page fron ano开发者_C百科ther in a div.

I tried using

<include file="NavigationTree.html" />

and

<? include("/starfix/pages/NavigationTree.html"); ?>

But it doesn't work. Am I doing something wrong or do i need to do it some other way?


You may want to consider using Server Side Includes (SSI).

You would place your HTML snippet into a separate file, such as NavigationTree.html, and then you would simply reference it in your web pages by using:

<!--#include virtual="NavigationTree.html" -->

SSI is supported by all the popular web servers, including Apache, IIS and lighttpd.


Note that if you are using a shared host, you may have to use the .shtml, .stm, or .shtm extension for SSI to work. If you have root access to your web server, it can be easily configured to enable SSI for any extension, including html.


This is not possible in pure HTML.

The former is a notation I have never seen before, it is not HTML, maybe it works in some specific server-side templating language.

The latter is PHP. It should work but you need to bear in mind include() works with absolute paths inside the server's file system.

You should specify a relative path:

<? include("./NavigationTree.html"); // will work if it's in the same directory ?>

or an absolute one that will probably look something like this:

<? include("/path/to/your/www/dir/starfix/pages/NavigationTree.html"); ?>

(ask your admin for the absolute path to your web root)

You can maybe also do a HTTP include:

but that's unwise because it tends to be slow, and generates a second request on each page request.

You can also use SSI as outlined by @Daniel.


You could also use jQuery for this,

e.g.

<div id="yourDiv" />
<script> 
$("#yourDiv").load("NameOfYourPageToReadFrom.ext #NameOfDivToReadFrom");
</script>

This puts the contents of the 'NameOfDivToReadFrom' DIV in the called file ''NameOfYourPageToReadFrom' into the loaded DIV ('yourDiv') in your current file.

Remember to add the definition to the header part of your html.

e.g.

<head>
       <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head> 


You can use an iframe for that, e.g.:

<iframe width="500" height="300" frameborder="no" scrolling="no" marginheight="0"   marginwidth="0" 
src="http://www.example.com/page.html"></iframe>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜