Javascript从url中将html读入字符串 [英] Javascript read html from url into string

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

问题描述

我目前正在建立一个能够作为ftp类浏览器的网站。基本上我所拥有的是一台带有一些图像的ftp服务器。



我无法弄清的是:如果我浏览到这个ftp站点,我可以查看源代码(在某些浏览器中看到),我需要的是以一种字符串方式(使用javascript)保存该源代码。



原因是,我会做出一种'图像'浏览器。我计划通过将源代码读入一个字符串来完成该操作,然后复制所有图像源并使用innerHTML创建一个新的布局。



总之:我想阅读信息从一个网址,并以不同的方式显示。






好吧,似乎无法得到它的工作。问题可能是我无法使用服务器端脚本。是否有可能把一个文件放在FTP服务器上,我可以加载它可以动态地将数据加载到同一文件夹中? (当我说FTP时,我实际上是指一个具有FTP访问权限的NAS服务器)。 解决方案

您的答案是Ajax。它可以从一个URL POST和GET数据,就像浏览一个网站一样,它会以字符串的形式返回HTML。



如果你打算使用 jQuery (真正方便),它很容易使用 Ajax 。像这个例子一样(没有图书馆就无法工作):

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

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

  var xmlhttp; 
if(window.XMLHttpRequest){//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else {//代码为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();


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.

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).

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.

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


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).

解决方案

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.

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);
    }
});

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从url中将html读入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆