Import code from extarnal file to HTML

2019-08-05 08:28发布

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

<h1>Text text text 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"?????

标签: html xhtml
3条回答
Bombasti
2楼-- · 2019-08-05 08:48

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.

查看更多
对你真心纯属浪费
3楼-- · 2019-08-05 08:50

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.

查看更多
Evening l夕情丶
4楼-- · 2019-08-05 08:59

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>
查看更多
登录 后发表回答