MyTetra Share
Делитесь знаниями!
get html code using javascript with a url
Время создания: 13.07.2018 15:30
Текстовые метки: javascript get html code
Раздел: Javascript

Use jQuery:


$.ajax({ url: 'your-url', success: function(data) { alert(data); } });

This data is your HTML.


Without jQuery (just JS):


function makeHttpObject() {

try {return new XMLHttpRequest();}

catch (error) {}

try {return new ActiveXObject("Msxml2.XMLHTTP");}

catch (error) {}

try {return new ActiveXObject("Microsoft.XMLHTTP");}

catch (error) {}


throw new Error("Could not create HTTP request object.");

}

var request = makeHttpObject();

request.open("GET", "your_url", true);

request.send(null);

request.onreadystatechange = function() {

if (request.readyState == 4)

alert(request.responseText);

};

shareimprove this answer

edited Apr 12 '17 at 4:04


card100

4317

answered Jun 16 '11 at 16:47


Senad Meškin

10.5k42547

@Senad Meskin thanks for your answer, but issit possible to do it with jQuery? i was wondering if there are other methods to do it. – simplified Jun 16 '11 at 16:49

@Senad Meskin thanks. i was trying to write this function in a empty html file that just put the code above in the script tag, however. it seems that the readystate is 1 and didn't get through. do you know why? i have changed to url to already. but it still doesn't work. – simplified Jun 16 '11 at 17:50

Does your url points to another server, if so that is the reason, security issue. – Senad Meškin Jun 16 '11 at 17:53

1

@Senad Meskin lets say it's google.com or youtube.com? is it possible? – simplified Jun 16 '11 at 17:54

1

No its not possible, only thing that you can is call your url, and on serverside code call www.google.com and write to response content of google.com – Senad Meškin Jun 16 '11 at 18:06

show 1 more comment

up vote

3

down vote

There is a tutorial on how to use ajax here: http://www.w3schools.com/ajax/default.asp


This is an example code taken from that tutorial:


<html>

<head>

<script type="text/javascript">

function loadXMLDoc()

{

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

}

</script>

</head>

<body>


<div id="myDiv"><h2>Let AJAX change this text</h2></div>

<button type="button" onclick="loadXMLDoc()">Change Content</button>


</body>

</html>

shareimprove this answer

answered Jun 16 '11 at 17:12


Guy

4,903235681

add a comment

up vote

1

down vote

For external (cross-site) solution, you can use: https://stackoverflow.com/a/18447625/2657601


It uses $.ajax() function, so it includes google jquery.

Так же в этом разделе:
 
MyTetra Share v.0.53
Яндекс индекс цитирования