Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load page content to variable

Good day.

I never really got a good hand at JavaScript, therefore this unusual and simple question.

How can i load a page content to a JavaScript variable, with the least amount of code, no framework, and the less impact possible on performance?

Thanks.


EDIT

Sorry guys. I forgot to mention: Get the page content from the specified url to a JS var.


Following Brendan Suggestion

I had already seen Brendan alternative elsewhere and tried it, but it didn't work at the time, and it doesn't work now. Meanwhile Firebug and the Browsers tested (IE8 and FF) don't report any error. So whats wrong?

like image 720
Fábio Antunes Avatar asked Nov 19 '10 20:11

Fábio Antunes


People also ask

How do you load a variable in HTML?

Use the <var> tag in HTML to add a variable. The HTML <var> tag is used to format text in a document. It can include a variable in a mathematical expression.

How do you load a page in JavaScript?

Approach: We can use window. location property inside the script tag to forcefully load another page in Javascript. It is a reference to a Location object that is it represents the current location of the document. We can change the URL of a window by accessing it.

What is a JavaScript variable?

Variable means anything that can vary. In JavaScript, a variable stores the data value that can be changed later on. Use the reserved keyword var to declare a variable in JavaScript.


1 Answers

This is a modified version of an example you can find at w3schools.com.

<script type="text/javascript">     function loadXMLDoc(theURL)     {         if (window.XMLHttpRequest)         {// code for IE7+, Firefox, Chrome, Opera, Safari, SeaMonkey             xmlhttp=new XMLHttpRequest();         }         else         {// code for IE6, IE5             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");         }         xmlhttp.onreadystatechange=function()         {             if (xmlhttp.readyState==4 && xmlhttp.status==200)             {                 alert(xmlhttp.responseText);             }         }         xmlhttp.open("GET", theURL, false);         xmlhttp.send();     } </script> 

So just make "example.html" be any sort of path (relative or absolute) to the page you want to load, and xmlhttp.responseText will be a string containing the response content. You can also use xmlhttp.responseXML if you want it to be stored as a traversable XML document. Anyway, just assign either of those to a variable of your choice, and you will have it!

Note that 'loadXMLDoc' does not return anything directly but defines one of its members ('onreadystatechange') to do that job, and to does it only in certain condition (readyState and status). Conclusion - do not assign the output of this function to any var. Rather do something like:

var xmlhttp=false; loadXMLDoc('http://myhost/mycontent.htmlpart'); if(xmlhttp==false){ /* set timeout or alert() */ } else { /* assign `xmlhttp.responseText` to some var */ } 

Without that, all one can see is 'undefined'...

like image 166
Brendan Avatar answered Oct 02 '22 14:10

Brendan