Tell me, how in javascript variable assign the value of the entire content by url?

    3 answers 3

    <script type="text/javascript"> function startAjax(url){ var request; if(window.XMLHttpRequest){ request = new XMLHttpRequest(); } else if(window.ActiveXObject){ request = new ActiveXObject("Microsoft.XMLHTTP"); } else { return; } request.onreadystatechange = function(){ switch (request.readyState) { case 4:{ if(request.status==200){ var foo = request.responseText; alert ( foo ); } else alert("Ошибка: сервер вернул статус: "+ request.status); break } } } request.open ('GET', url, true); request.send (''); } window.onload = startAjax('url'); </script> 

    In the row window.onload = startAjax('url'); , url - replace with address

    • it will be impossible to work on another domain so to say with the address vk.com - zb '
    • The jquery package $ .ajax function supports cross-domain queries: the apDo.jjery.com/jQuery.ajax property crossDomain - deivan_
    • 3
      cross-domain queries do not work, if cors is absent, show me $ .ajax () request to vk.com - zb '
    • Then such a request to strain your web server. Let him take a page and give it to the one who asks. - alexlz
    • @eicto Why not? It worked for me, see for yourself. Only to vk.com I have not tried. - Saturn
     var foo = window.location.href; 
    • does not fit - Saturn
     var foo = window.location 

    foo will be equal to the URL

    • Not quite understand the question. I need a variable to assign the page content to a specific address. - Saturn
    • @Saturn in the same domain? - zb '
    • Not. I apparently incorrectly formulated the question. The value of the variable must be obtained from the content of the page at a specific address - Saturn
    • 3
      impossible, except when a specific address is also controlled by you (cors) - zb '
    • one
      Absolutely, google same origin policy - zb '