How can I get an HTML page (.html) to read the contents of a text document that can be found in the same folder as the .html file? The server is IIS.
如何获取HTML页面(.html)来读取可以在与.html文件相同的文件夹中找到的文本文档的内容?服务器是IIS。
Thanks
6 个解决方案
#2
It seems like you can use #include directives in IIS.
看起来你可以在IIS中使用#include指令。
http://msdn.microsoft.com/en-us/library/ms525185.aspx
But to be honest I strongly suggest using a scripting language, either PHP or something in the ASP family.
但说实话,我强烈建议使用脚本语言,PHP或ASP系列中的某些东西。
#3
one hesitates to suggest iframes, but out of completeness...
一个人犹豫建议iframe,但出于完整性......
(You probably need server side includes, but you probably have bigger issues in general)
(您可能需要服务器端包含,但您可能通常会遇到更大的问题)
#4
By adding the following JavaScript code to the element of the web page:
通过将以下JavaScript代码添加到网页的元素:
<script>
function clientSideInclude(id, url)
{
var req = false;
// For Safari, Firefox, and other non-MS browsers
if (window.XMLHttpRequest)
{
try {
req = new XMLHttpRequest();
} catch (e) {
req = false;
}
} else if (window.ActiveXObject) {
// For Internet Explorer on Windows
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
req = false;
}
}
}
var element = document.getElementById(id);
if (!element) {
alert("Bad id " + id +
"passed to clientSideInclude." +
"You need a div or span element " +
"with this id in your page.");
return;
}
if (req) {
// Synchronous request, wait till we have it all
req.open('GET', url, false);
req.send(null);
element.innerHTML = req.responseText;
} else {
element.innerHTML =
"Sorry, your browser does not support " +
"XMLHTTPRequest objects. This page requires " +
"Internet Explorer 5 or better for Windows, " +
"or Firefox for any system, or Safari. Other " +
"compatible browsers may also exist.";
}
}
</script>
#5
IIS can do server-side includes. BUt, if you can't do this, and want to include the text file in the HTML, you could grab the file with an XMLHTTPRequest object and insert it into the DOM with Javascript.
IIS可以做服务器端包含。 BUt,如果你不能这样做,并希望在HTML中包含文本文件,你可以使用XMLHTTPRequest对象获取文件并使用Javascript将其插入DOM。
Naturally, a JS library will make this easier. For example in prototype:
当然,JS库会使这更容易。例如在原型中:
new Ajax.Updater($('id_of_div'), 'http://yourdomain/path/to/file.txt');
that would grab the file and drop the contents into <div id="id_of_div"></div>
这将获取文件并将内容放入
Or in jQuery:
或者在jQuery中:
$("#id_of_div").load("http://yourdomain/path/to/file.txt");
#6
You can put an url to the text file directly, the user will download it by clicking. If this is not what you want, then you can probably use Server Side Includes (see here and here). If this does not work, you must write a script (ASP?) to read the file.
您可以直接将URL添加到文本文件中,用户可以通过单击下载它。如果这不是您想要的,那么您可以使用服务器端包含(请参阅此处和此处)。如果这不起作用,您必须编写一个脚本(ASP?)来读取该文件。
#1
Google for server-side includes.
谷歌服务器端包括。
#2
It seems like you can use #include directives in IIS.
看起来你可以在IIS中使用#include指令。
http://msdn.microsoft.com/en-us/library/ms525185.aspx
But to be honest I strongly suggest using a scripting language, either PHP or something in the ASP family.
但说实话,我强烈建议使用脚本语言,PHP或ASP系列中的某些东西。
#3
one hesitates to suggest iframes, but out of completeness...
一个人犹豫建议iframe,但出于完整性......
(You probably need server side includes, but you probably have bigger issues in general)
(您可能需要服务器端包含,但您可能通常会遇到更大的问题)
#4
By adding the following JavaScript code to the element of the web page:
通过将以下JavaScript代码添加到网页的元素:
<script>
function clientSideInclude(id, url)
{
var req = false;
// For Safari, Firefox, and other non-MS browsers
if (window.XMLHttpRequest)
{
try {
req = new XMLHttpRequest();
} catch (e) {
req = false;
}
} else if (window.ActiveXObject) {
// For Internet Explorer on Windows
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
req = false;
}
}
}
var element = document.getElementById(id);
if (!element) {
alert("Bad id " + id +
"passed to clientSideInclude." +
"You need a div or span element " +
"with this id in your page.");
return;
}
if (req) {
// Synchronous request, wait till we have it all
req.open('GET', url, false);
req.send(null);
element.innerHTML = req.responseText;
} else {
element.innerHTML =
"Sorry, your browser does not support " +
"XMLHTTPRequest objects. This page requires " +
"Internet Explorer 5 or better for Windows, " +
"or Firefox for any system, or Safari. Other " +
"compatible browsers may also exist.";
}
}
</script>
#5
IIS can do server-side includes. BUt, if you can't do this, and want to include the text file in the HTML, you could grab the file with an XMLHTTPRequest object and insert it into the DOM with Javascript.
IIS可以做服务器端包含。 BUt,如果你不能这样做,并希望在HTML中包含文本文件,你可以使用XMLHTTPRequest对象获取文件并使用Javascript将其插入DOM。
Naturally, a JS library will make this easier. For example in prototype:
当然,JS库会使这更容易。例如在原型中:
new Ajax.Updater($('id_of_div'), 'http://yourdomain/path/to/file.txt');
that would grab the file and drop the contents into <div id="id_of_div"></div>
这将获取文件并将内容放入
Or in jQuery:
或者在jQuery中:
$("#id_of_div").load("http://yourdomain/path/to/file.txt");
#6
You can put an url to the text file directly, the user will download it by clicking. If this is not what you want, then you can probably use Server Side Includes (see here and here). If this does not work, you must write a script (ASP?) to read the file.
您可以直接将URL添加到文本文件中,用户可以通过单击下载它。如果这不是您想要的,那么您可以使用服务器端包含(请参阅此处和此处)。如果这不起作用,您必须编写一个脚本(ASP?)来读取该文件。