when we use AJAX to call jsp, then the content in jsp will be composed in current page, but the JS in the jsp file can not be parsed.
So we need to abstract the JS to a separate file, then include the js file in the HEAD of current page.
you can refer to JS function appendSpecificJS() below.
As you know, AJAX is an Asynchronous call, so we need to wait for some time to get the content. If we want to get the element value in the JSP file, we need to use window.setTimeout(functionName, delay).
var xmlhttp = getXMLHttpRequest();
function getXMLHttpRequest() {
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
} else {// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
function ajaxValueChange(areaName, errMsg, url) {
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
document.getElementById(areaName).innerHTML = xmlhttp.responseText;
showHint('');
appendSpecificJS();
} else {
showHint(errMsg);
}
}
}
xmlhttp.open("POST", url, true);
xmlhttp.send();
}
function baseAction() {
// ajax call here
ajaxValueChange("specificProduct", 'Failed to load specific Product.', "./specificProduct.jsp?servername=" + servername);
// var delayInterval = 1000;
// window.setTimeout(appendSpecificJS, delayInterval);
}
function appendSpecificJS() {
var head = document.getElementsByTagName('HEAD').item(0);
var script = document.createElement("script");
// <input type="hidden" id="jsfilename" name="jsfilename" value="<%=jsFileName %>" /> in [specificProduct.jsp] file
script.src = document.getElementById("jsfilename").value;
script.type="text/javascript";
script.charset="utf-8";
if (isLoadJS) {
head.replaceChild(script, oldNode);
} else {
head.appendChild(script);
}
isLoadJS = true;
oldNode = script;
}
You also can put appendSpecificJS(); in the Ajax response successfully segment to avoid use Timer.
There are two points we should pay attention to:
1) the JS file path can NOT be under /web-root
2) we should fix the JS function name in sepecific product JS, so the base js can invoke them.
eg. specificParam() is defined in specific js file.
To avoid JS error in explorer, when we call specificParam(), we need to add if condition.
function baseSave() {
if (isSaved) {
showHint("This is already saved successfully.");
return;
}
clearHint();
var url = window.document.location.href;
var pos = url.indexOf("?");
var params = url.substr(pos);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
showHint(xmlhttp.responseText);
isSaved = true;
loadReport();
} else {
isSaved = false;
showHint('Failed to save.');
}
}
}
var specificParam = '';
if (isLoadJS) {
specificParam = specificParam();
}
xmlhttp.open("POST", "./persistence.jsp" + params + specificParam, true);
xmlhttp.send();
}
分享到:
相关推荐
Beginning JavaScript with DOM Scripting and Ajax is an essential resource for modern JavaScript programming. This completely updated second edition covers everything you need to know to get up-to-...
AJAX 的核心是JavaScript的XMLHttpRequest对象,这个对象允许JavaScript在后台与服务器进行通信。在传统的Web应用中,用户通过表单提交数据,浏览器会发送HTTP请求到服务器,服务器处理请求并返回一个全新的页面。...
JavaScript作为Web前端的核心语言,DOM(Document Object Model)则提供了对HTML和XML文档的结构化表示,而Ajax(Asynchronous JavaScript and XML)则革新了网页的交互体验,实现了页面的异步更新。 一、...
Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition (pdf + ePub) by Russ Ferguson and Christian Heilmann Publisher: Apress; 2nd Edition (July 2013) Language: English ISBN-10: 1430250925 ...
这份"Learn_JavaScript_and_Ajax_with_w3Schools.zip"压缩包包含了一个电子书和一个文本文件,旨在帮助学习者掌握这两种技术的核心概念。 JavaScript,全称ECMAScript,是一种轻量级的解释型编程语言,主要用于网页...
Ajax,全称Asynchronous JavaScript and XML(异步JavaScript和XML),是一种创建动态网页的技术,允许在不重新加载整个页面的情况下,与服务器交换数据并更新部分网页内容。在"JavaScript语言与Ajax应用(第二版)...
### AJAX(Asynchronous JavaScript and XML)技术 #### 定义与概述 AJAX,全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是一种用于创建交互式网页应用的网页开发技术。它允许网页在无需...
AJAX (Asynchronous JavaScript And XML)
《JavaScript和AJAX_视觉快速入门指南》是一本专为初学者和中级开发者设计的教程,旨在帮助读者深入理解和掌握JavaScript编程语言以及AJAX(异步JavaScript和XML)技术。这本书通过直观、易于理解的方式,提供了丰富...
**Ajax(Asynchronous JavaScript and XML)**是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。它通过在后台与服务器进行少量数据交换,使网页实现异步更新。这种技术的核心在于JavaScript,允许...
**Ajax(Asynchronous JavaScript and XML)**是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。它通过在后台与服务器进行少量数据交换,使网页实现异步更新。这一技术的核心在于JavaScript,它允许...
"【AJAX】传统JavaScript实现AJAX的小栗子" 这个标题表明我们将探讨的是使用原生JavaScript实现AJAX(Asynchronous JavaScript and XML)的技术。AJAX是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术...
(2)新建好页面之后,如果要使用Ajax,那么先在页面上增加一个ScriptManager控件,然后再增加一个UpdatePanel控件. (3)将要实现无刷新的控件,放到UpdatePanel控件中即可. 3.注意事项: 首先要确定的是,AJAX并不是真正的...
这本书的标题《Django JavaScript AJAX and jQuery使用教程》透露了其核心内容,即向读者介绍如何在Django框架中集成JavaScript,特别是通过使用AJAX和jQuery这两个强大的工具来创建Web应用程序。AJAX(Asynchronous...