Javascript 将 html 从 url 读入字符串

Javascript read html from url into string(Javascript 将 html 从 url 读入字符串)

本文介绍了Javascript 将 html 从 url 读入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建一个应该能够用作 ftp 浏览器的站点.基本上我有一个 ftp 服务器,上面有一些图像.

I'm currently building a site that should be able to function as a ftp sort of browser. Basically what I have is a ftp server with some images on it.

我想不通的是:如果我浏览到这个 ftp 站点,我可以查看 ftp 站点的源(如在某些浏览器中所见),我需要以某种方式将该源保存为字符串(使用 javascript).

What I can't figure out is: if I browse to this ftp site I can view the source of the ftp site (as seen in some browser), what I need is to save that source in a way to a string (using javascript).

原因是,我将制作某种图像"浏览器.我计划通过将源读入一个字符串来实现这一点,然后复制所有图像源并使用 innerHTML 创建一个新布局.

The reason is, that I will make som kind of 'image' browser. I plan on accomplishing that by reading the source into a string, then copy all the image sources and use innerHTML to create a new layout.

简而言之:我想从一个 url 读取信息并以不同的方式显示它.

In short: I want to read information from a url and display it in a different way.

好吧,似乎无法让它工作.问题可能是我不能使用服务器端脚本.但是,是否可以在我可以加载的 ftp 服务器上放置一个文件,该文件可以动态加载同一文件夹中的数据?(当我说 FTP 时,我实际上是指具有 FTP 访问权限的 NAS 服务器).

Well, can't seem to get it working. The problem might be that I cannot use serverside scripting. Would it be possible however to put a file on the ftp server that I can load that can dynamically load the data in the same folder? (when I say FTP I actually mean a NAS server with FTP access).

推荐答案

你的答案是 Ajax.它可以从 URL POST 和 GET 数据,就像浏览网站一样,它会将 HTML 作为字符串返回.

Your answer is Ajax. It can POST and GET data from an URL, just like browsing a website, and it will return the HTML as a string.

如果你打算使用 jQuery(非常方便),使用 Ajax.像这个例子(没有库不工作):

If you plan on using jQuery (real handy), it is easy to use Ajax. Like this example (does not work without the library):

$.ajax({
    url : "/mysite/file.html",
    success : function(result){
        alert(result);
    }
});

如果您想使用默认 Javascript,请查看 http://www.w3schools.com/ajax/default.asp

If you want to use default Javascript, take a look at http://www.w3schools.com/ajax/default.asp

var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else { // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
    }
}
xmlhttp.open("GET", "ajax_info.txt", true);
xmlhttp.send();

这篇关于Javascript 将 html 从 url 读入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Javascript 将 html 从 url 读入字符串

基础教程推荐