简体   繁体   English

如何从http网站加载元素并将其添加到我的网站

[英]How do I load elements from a http site and add it to my website

We want to load html element from a url and add it to our website (or replace a current element) 我们要从url加载html元素并将其添加到我们的网站(或替换当前元素)

We have tried: 我们尝试过:

document.getELementById("content").innerHTML = url;

This works with images but we want to load just an element not the whole page 这适用于图像,但我们只想加载一个元素而不是整个页面

Cheers! 干杯!

You're going to have to make an HTTP request to the server and get the content first. 您将必须向服务器发出HTTP请求并首先获取内容。

var request = new XMLHttpRequest();
var url = 'your-url-here';

request.onload = function(evt) {
    document.getElementById('content').innerHTML = request.responseText;
};

request.open('GET', url, true);
request.send();

You can use jQuery with load() to achieve something like that after asking for permission from the admins of the site where you're stealing from. 从您要窃取的站点的管理员处寻求许可后,可以将jQuery与load()以实现类似的目的。

"Stealing" might sound a bit harsh for you but that's how the admins of the other site will see it. “窃听”对您来说可能听起来有些苛刻,但这是另一个站点的管理员将如何看待它。 If they notice what you're doing, your server will be added to their firewall under the BLOCKED section. 如果他们注意到您在做什么,您的服务器将被添加到其防火墙的“已BLOCKED部分下。

Note that if the other site is running on a different domain, trying to do this JavaScript will fail because of the Same-origin Policy . 请注意,如果另一个站点在不同的域上运行,则由于“ 同源策略” ,尝试执行此JavaScript将会失败。 In that case, you'll have to add code to your own server which gets the data from the other site and offer it in a useful form to your own JavaScript running in the client (like using jQuery's load() function). 在这种情况下,您必须将代码添加到自己的服务器,该服务器从另一个站点获取数据,并以有用的形式将其提供给客户端中运行的JavaScript(例如使用jQuery的load()函数)。

If your server is written in JavaScript, Apache HttpComponents is your friend. 如果您的服务器是用JavaScript编写的,则Apache HttpComponents是您的朋友。

好吧,您可以仅使用检查元素工具复制代码,但这被认为是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM