| | |
Javascript problem in Ajax
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 7
Reputation:
Solved Threads: 0
clienthint.js
test.jsp
index.jsp
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!
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<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!
•
•
Join Date: Sep 2009
Posts: 45
Reputation:
Solved Threads: 8
0
#2 22 Days Ago
just put one debugger and check its executing or not
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function myfunc1() { debugger; alert("button click"); }
Chandru
SilverlightScripting.com
SilverlightScripting.com
0
#6 13 Days Ago
So when you do alert(xmlhttp.responseText) you get the script-tags as well? Then try this:
JavaScript Syntax (Toggle Plain Text)
function evalScript(html, div) { var script = ''; var newHtml = html.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function() { scripts += arguments[1] + '\n'; return ''; }); div.innerHTML = newHtml; eval(scripts); }
Last edited by Alxandr; 13 Days Ago at 1:28 pm.
•
•
Join Date: Nov 2009
Posts: 7
Reputation:
Solved Threads: 0
0
#7 11 Days Ago
Yes , alert(xmlhttp.responseText) will get the script-tags as well.
But I don't how to test with your function ? how to place and use it in my examples above?
But I don't how to test with your function ? how to place and use it in my examples above?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var script = ''; // or var scripts = ''; ?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
arguments[1] // what is it ?
Last edited by manofspider; 11 Days Ago at 12:15 am.
0
#8 11 Days Ago
•
•
•
•
clienthint.js
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function stateChanged() { if (xmlhttp.readyState == 4) { document.getElementById("txtHint").innerHTML = xmlhttp.responseText; } }
JavaScript Syntax (Toggle Plain Text)
evalScript(xmlhttp.responseText, document.getElementById('txtHint'));
•
•
•
•
JavaScript Syntax (Toggle Plain Text)
function evalScript(html, div) { var script = ''; var newHtml = html.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function() { scripts += arguments[1] + '\n'; return ''; }); div.innerHTML = newHtml; eval(scripts); }
•
•
•
•
Yes , alert(xmlhttp.responseText) will get the script-tags as well.
But I don't how to test with your function ? how to place and use it in my examples above?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var script = ''; // or var scripts = ''; ?
If you don't understand that I suggest you look up JavaScript, because it's something you as a webdeveloper should know. This line only mean the second argument of the function, but it's probably a good idea to get a understanding about how functions work in JavaScript.
HTH
•
•
Join Date: Nov 2009
Posts: 7
Reputation:
Solved Threads: 0
0
#9 10 Days Ago
update state-Changed function:
Add new function:
debug: alert('html : ' + '\n' + html);
debug: alert('newHtml : ' + '\n' + newHtml);
debug: alert('scripts : ' + '\n' + scripts);
debug: alert('div.innerHTML : ' + '\n' + div.innerHTML);
Test Flow:
1. Click on 'Click to Alert'
2. Not work -> Error Console ( Error: myfunc1 is not defined )
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function stateChanged() { if (xmlhttp.readyState == 4) { //document.getElementById("txtHint").innerHTML = xmlhttp.responseText; evalScript(xmlhttp.responseText, document.getElementById('txtHint')); } }
Add new function:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function evalScript(html, div) { alert('html : ' + '\n' + html); var scripts = ''; var newHtml = html.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function() { scripts += arguments[1] + '\n'; return ''; }); alert('newHtml : ' + '\n' + newHtml); alert('scripts : ' + '\n' + scripts); div.innerHTML = newHtml; alert('div.innerHTML : ' + '\n' + div.innerHTML); eval(scripts); }
debug: alert('html : ' + '\n' + html);
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
html : this is test.jsp <br> <script> function myfunc1() { alert("button click"); } </script> <input name="mybutton" value="Click to Alert" type="button" onclick="myfunc1();">
debug: alert('newHtml : ' + '\n' + newHtml);
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
newHtml : this is test.jsp <br> <input name="mybutton" value="Click to Alert" type="button" onclick="myfunc1();">
debug: alert('scripts : ' + '\n' + scripts);
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
scripts : <script> function myfunc1() { alert("button click"); } </script>
debug: alert('div.innerHTML : ' + '\n' + div.innerHTML);
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
div.innerHTML : this is test.jsp <br> <input name="mybutton" value="Click to Alert" onclick="myfunc1();" type="button" >
Test Flow:
1. Click on 'Click to Alert'
2. Not work -> Error Console ( Error: myfunc1 is not defined )
0
#10 10 Days Ago
•
•
•
•
debug: alert('scripts : ' + '\n' + scripts);
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
scripts : <script> function myfunc1() { alert("button click"); } </script>
Just to make shure I didn't mistype something; here is a direct copy of working code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var newHtml = html.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function() { script += arguments[1]; return ''; });
Somewhere in that line there has to be an error, though I can't find it, but this one is working for me (just tested it). If that doesn't work I'm afraid I don't know how to help you, because as said it's working here (I even tried with your html).
HTH
![]() |
Similar Threads
- javascript problem....... (JavaScript / DHTML / AJAX)
- Javascript Problem with Ajax Respons (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: How Disable The F5 Key In Mozilla
- Next Thread: grey out the page while showing the loading image
| Thread Tools | Search this Thread |
.net adobe adobeacrobat ajax alphafive amf android api array asp asp.net avg beta box browser bug bugs c# c++ cart class close code codebox codeinjection codes compatible createrange() css database debugger dependent developer development dom eclipse element error explorer external findcontrol firefox flash glitch google gui hiddenvalue highlightedword html ie8 iframe images internet java javascript jquery jsf jsp listbox masterpage math method microsoft mosaic mouse mp4 msdn news office pdf php post practice preview print problem processor python registry ruby runtime scroll security shopping size software sql sticky stop subdomain text textfield threads treeview validation vista w3c web zend zeroday






