In the div site example1.ru need to insert a div from the site example2.ru .

I wrote this code, but for some reason it does not work.
What could be the problem?

 <div id="vip_view_div_cross"></div> 
 $("#vip_view_div_cross").load("example2.ru #vip_view_div"); 
  • one
    this is parsing, usually in php, and on ajax, only loading is done - user33274
  • 2
    In the ban on cross-domain access without explicit permission from the second site. - Alexey Ten

2 answers 2

 getElement('http://ru.stackoverflow.com', '#nav-questions', function(element) { console.log(element); }); getElement('http://ru.stackoverflow.com', '.question-hyperlink', function(element) { console.log(element.innerHTML); }); function getElement(url, selector, c) { request(new XMLHttpRequest()); function request(xhr) { xhr.open('GET', 'https://crossorigin.me/' + url, true); xhr.send(); xhr.onreadystatechange = function() { if(xhr.readyState == 4) { if(xhr.status == 200) { html = document.createElement('div'); html.innerHTML = xhr.responseText; c(html.querySelector(selector)); } } } } } 

jquery

 $('#q').load('https://crossorigin.me/http://ru.stackoverflow.com #nav-questions'); 
 <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script> <div id='q'></div> 

  • one
    Thank. You helped me a lot! - Rumato
  • I hope the next question will not be "And how do I login to example2.com" ... - Alexey Ten
  • No, in fact, I realized that this way is not quite correct (it is not always stable, everything will work in my case), but the answer itself helped me to understand the topic. - Rumato
  • Already does not work, apparently crossorigin.me covered up) - Mr. Black

Celal in the form of a bookmarklet - calculating a kilogram of gold in rubles thanks # Mr.Black

 javascript:getElement('https://www.cbr.ru/', '#widget_metal', function(element) { alert(parseFloat(element.getElementsByClassName('nowrap')[2].innerHTML.replace(/\s/g, '').replace(/,/,'.'))*1000 + "%D1%80%D1%83%D0%B1%D0%BB%D0%B5%D0%B9"); }); function getElement(url, selector, c) { request(new XMLHttpRequest()); function request(xhr) { xhr.open('GET', 'https://crossorigin.me/' + url, true); xhr.send(); xhr.onreadystatechange = function() { if(xhr.readyState == 4) { if(xhr.status == 200) { html = document.createElement('div'); html.innerHTML = xhr.responseText; c(html.querySelector(selector)); } } } } };