I want to view source code of current page using Javascript, so I use this snippet code:
function getSourceCode() {
var url="http://localhost:8080/java/",xmlhttp;//Remember, same domain
if("XMLHttpRequest" in window)
xmlhttp = new XMLHttpRequest();
if("ActiveXObject" in window)
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open('GET',url,true);
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4){
var a = xmlhttp.responseText;
alert(a);
document.getElementById("demo").innerHTML = a;
}
};
xmlhttp.send(null);
}
And in HTML file, I declared a line
to print out the variable stored all source code like it alert to screen. But it always alert to my screen without print to variable "a" in the html screen.This is my HTMl code:
<!DOCTYPE html>
<html>
<head>
<title>Vo Tinh Thuong</title>
<script src="code.js"></script>
</head>
<body>
<input id="clickMe" type="button" value="clickme" onclick="getSourceCode();" />
<p id="demo"></p>
</body>
</html>
I just want to print out the variable to html webpage, not alert it like that.