问题
I have written sample web application with servlets and jsp. As per the below code the action url should be executed automatically according to the "onload". I have written this below code in one of my servlet, The below "onload" form hits the path in action only in Firefox and IE but not in Chrome(41.0.2272.74 beta-m (64-bit)).I have seen the link Chrome - is not working but i did not get proper idea, I have my below code in the end of servelt. there is nothing to execute after this code in the servlet.
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> \n");
out.write("<html><body onload=\"alert('alert has been fired');document.forms[0].submit()\">\n");
out.write("<form method=\"POST\" action=\"" + actionUrl + "\">\n");
out.write("<input type=\"hidden\" name=\"resourcePath\" value=\"" + callbackUrl + "\"/>\n");
out.write("<input type=\"hidden\" name=\"locale\" value=\"" + locale + "\"/>\n");
out.write("<input type=\"hidden\" name=\"rspjRequest\" value=\"" + true + "\"/>\n");
out.write("</form>\n</body>\n</html>\n");
out.close();
Is there any reserved key words of chrome in the above action url?
回答1:
After some investigation finally i got solution for my problem. I forgot to set the Content type for the response in the servlet.
response.setContentType("text/html; charset=UTF-8");
Chrome browser will display response in the form of html page by using above line.
回答2:
You can try function in script tag of that html page.
<script>
function myfunction() {
alert("This is the first.");
}
window.onload = function()
{
myfunction;
}
</script>
来源:https://stackoverflow.com/questions/29003605/html-body-onload-is-not-working-in-chrome