简体   繁体   中英

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)

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.

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.

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

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

If your server is written in JavaScript, Apache HttpComponents is your friend.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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