var xmlhttp;
var parameters="";
function showHint(url) {
var myform = document.forms[0];
if (myform != undefined) {
parameters = getRequestBody(myform);
}
if (url.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
}
xmlhttp = GetXmlHttpObject();
if (xmlhttp == null) {
alert("Your browser does not support XMLHTTP!");
return;
}
xmlhttp.onreadystatechange = stateChanged;
xmlhttp.open("POST", url, true);
xmlhttp.send(parameters);
}
function stateChanged() {
if (xmlhttp.readyState == 4) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
function GetXmlHttpObject() {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject) {
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
function getRequestBody(oForm) {
var aParams = new Array();
for (var i=0 ; i < oForm.elements.length; i++) {
var sParam = encodeURIComponent(oForm.elements[i].name);
sParam += "=";
sParam += encodeURIComponent(oForm.elements[i].value);
aParams.push(sParam);
}
return aParams.join("&") + "&sid=" + Math.random();
}
test.jsp
this is test.jsp <br>
<script>
function myfunc1() {
alert("button click");
}
</script>
<input name="mybutton" value="Click to Alert" type="button" onclick="myfunc1();">
index.jsp
<html>
<head>
<script src="clienthint.js"></script>
</head>
<body>
Test Ajax <br><br>
<input type="button" value="test.jsp" onclick="showHint('test.jsp');"/>
<hr/>
<div id="txtHint" style="background:gray;height:200px;"></div>
</body>
</html>
Test Flow 1:
. When open page test.jsp directly
. Click on button "Click to Alert"
. Message "button click" appears
Test Flow 2:
. When open page index.jsp
. Click on button "test.jsp"
. Contents of DIV appears (contents of test.jsp)
Question (on Test Flow 2):
1. When Click on button "Click to Alert" at DIV section, but the message "button click" doesn't appears ?
2. Why JavaScript on test.jsp doesn't work ?
3. Is this JavaScript problem through Ajax ?
4. By keeping the function myfunc1() in test.jsp, how to make this function myfunc1() worked at side of index.jsp page?
Thanks in advance!