By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,729 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,729 IT Pros & Developers. It's quick & easy.

Ajax function not defined

P: 2
Hello! I have an Ajax script wich works well on a page, but when I use it in another page I got the error "function not defined"

this one works well :

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function details(var1, var2)
  3. {
  4.     var html;
  5.  
  6.     xmlhttp = new XMLHttpRequest();
  7.  
  8.     xmlhttp.onreadystatechange=function()
  9.     {
  10.         h = document.getElementById("log").innerHTML;        
  11.         h = h + xmlhttp.readyState + " / " + xmlhttp.status + "<br/>";
  12.         if(xmlhttp.readyState==4 && (xmlhttp.status==200||xmlhttp.status==0))
  13.         {
  14.             html = xmlhttp.responseText;
  15.             document.getElementById("res").innerHTML=html;
  16.         }
  17.     }
  18.     xmlhttp.open("GET","detailsCity?Pays="+var1+"&Name="+var2,true);
  19.     xmlhttp.send();
  20. }
  21. </script>
Expand|Select|Wrap|Line Numbers
  1.   <a href=""onMouseOver= "details('${CDet.res.name}','${CDet.res.cap}')" 
  2.               onMouseOut="details('${CDet.res.name}','${CDet.res.cap}')">
  3.               ${CDet.res.cap}</a><br>
  4.  
But when I put it in this page (I give the entier page here after) i got the "not defined" error:


Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <%@ page 
  4. language="java" 
  5. contentType="text/html; charset=UTF-8"
  6. pageEncoding="UTF-8"
  7. %>
  8. <%@ page import="java.awt.*"  %>
  9. <jsp:useBean id="CList" scope="session" class="fr.n7.asi.cs.model.CLBean"></jsp:useBean>    
  10.  
  11. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  12. <html>
  13.  
  14. <head>
  15. <link rel="stylesheet" type="text/css" href="sts.css" />
  16. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  17.  
  18.  
  19.  
  20. <script type="text/javascript">
  21. function details(var1, var2)
  22. {
  23.     var html;
  24.  
  25.     xmlhttp = new XMLHttpRequest();
  26.  
  27.     xmlhttp.onreadystatechange=function()
  28.     {
  29.         h = document.getElementById("log").innerHTML;        
  30.         h = h + xmlhttp.readyState + " / " + xmlhttp.status + "<br/>";
  31.         if(xmlhttp.readyState==4 && (xmlhttp.status==200||xmlhttp.status==0))
  32.         {
  33.             html = xmlhttp.responseText;
  34.             document.getElementById("res").innerHTML=html;
  35.         }
  36.     }
  37.     xmlhttp.open("GET","detailsCity?Pays="+var1+"&Name="+var2,true);
  38.     xmlhttp.send();
  39. }
  40. </script>
  41.  
  42.  
  43.  
  44. <title>CountryList View (JSP)</title>
  45. </head>
  46.  
  47. <body background="http://bytes.com/images/terres.jpeg">
  48.  
  49. Bonjour <% out.println(session.getAttribute("nom"));%> ! <a href="deconnect">se deconnecter</a><br><br><br>
  50.  
  51. <h1>LISTE DES PAYS COMMENCANT PAR PREFIXE</h1>
  52.  
  53. <table border="1">
  54. <c:forEach var="citem" items="${CList.res}">
  55. <tr><td>
  56. <a href="details?Code=${citem.code}">${citem.name}</a><br/>
  57. </td></tr>
  58. </c:forEach>
  59.  
  60. <tr><td>
  61.   <a href=""onMouseOver= "details('aaaa','bbbb')" 
  62.               onMouseOut="details('aaaa','aaaa')">
  63.               aaaaaa</a><br>
  64.  </td></tr>    
  65.  
  66.  
  67. </table>
  68. <h3><a href="prefCreate.jsp">retour au menu principal</a><br/></h3>
  69. </body>
  70. </html>
  71.  
  72.  
do somenone see what is happening with my code ?
thx a lot
May 22 '11 #1
Share this Question
Share on Google+
3 Replies


100+
P: 1,059
In your code line 29 and 30 is useless moreover generating the errors

And your line 34 is seeking for a object with id res
May 23 '11 #2

P: 2
ok thx I modified according to your remarks but it was still not working.
I solved the problem, actually I put both javascript scripts in the first jsp page (before I had the first jsp calling a second one, both had ajax code (two separated functions)). I am not sure it is possible to put some javascript functions in some code returned by the XMLhttpRequest objet... i have to hunt on this.
May 23 '11 #3

100+
P: 1,059
Yes Very much possible,
But its little tricky
To do that you will have to separate the javascript code and then create a new script object and the write those code to that object, or you can simply use JQuery
as example

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function send_req(uri)
  3. {
  4.  $.ajax(
  5.   {
  6.    url: "targepage?parameter=parameter", 
  7.    type: 'POST', 
  8.    dataType: 'html', 
  9.    data: uri, 
  10.    timeout: 90000,
  11.    success: function(html, data)
  12.        { 
  13.      $("#Reply").html(html);}
  14.     });
  15. }
  16. </script>
  17. <div id='Reply'>
  18. </div>
  19.  
May 24 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.