开发者

Import code from extarnal file to HTML

Say I have a text file (test.txt) that contains only the following line:

<h1>Text text te开发者_开发技巧xt text text</h1>

Is there any command that I can call the contents from this text file in an HTML document, so the contents in the text file imports where the call is made, every time the side shows?

Example: ?????"/test.txt"?????


I believe you mean to ask whether there exists a "command" in HTML which allows you to include a file.

In pure HTML by itself there does not, but the Apache server-side includes does provide such a directive:

<!--#include virtual="./test.txt" -->

You will need to enable SSI processing by your webserver. In Apache, you'd typically call your file .shtml or something like that.


What you're looking for is the concept call "Server Side Includes". Different servers will do this different ways, so you'll need to look at what your server provides.


Not with pure HTML, but you can with PHP (and almost every other server side language):

<?php include("test.txt"); ?>

Or you can do it in a roundabout way with JavaScript if the file is part of the website, you're actually running a web server, and you're not worrying about older browsers:

<script type="text/javascript">
var ajaxRq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
ajaxRq.open("GET", "test.txt", false);
ajaxRq.send(null);
document.write(ajaxRq.responseText);
</script>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜