MyTetra Share
Делитесь знаниями!
get html code using javascript with a url
Время создания: 13.07.2018 15:30
Текстовые метки: javascript get html code
Раздел: Javascript
Запись: Velonski/mytetra-database/master/base/1529901959y5xisb0eej/text.html на

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();"GET", "your_url", true);


request.onreadystatechange = function() {

if (request.readyState == 4)



shareimprove this answer

edited Apr 12 '17 at 4:04



answered Jun 16 '11 at 16:47

Senad Meškin


@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


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


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

show 1 more comment

up vote


down vote

There is a tutorial on how to use ajax here:

This is an example code taken from that tutorial:



<script type="text/javascript">

function loadXMLDoc()


var xmlhttp;

if (window.XMLHttpRequest)

{// code for IE7+, Firefox, Chrome, Opera, Safari

xmlhttp=new XMLHttpRequest();



{// code for IE6, IE5

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");




if (xmlhttp.readyState==4 && xmlhttp.status==200)










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

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



shareimprove this answer

answered Jun 16 '11 at 17:12



add a comment

up vote


down vote

For external (cross-site) solution, you can use:

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

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