just put one debugger and check its executing or not
function myfunc1() {
debugger;
alert("button click"); }
chandru7
Junior Poster in Training
72 posts since Sep 2009
Reputation Points: 9
Solved Threads: 13
At clienthint.js, before line 30, try adding: alert(xmlhttp.responseText);
Alxandr
Junior Poster in Training
73 posts since May 2009
Reputation Points: 15
Solved Threads: 10
So when you do alert(xmlhttp.responseText) you get the script-tags as well? Then try this:
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);
}
Alxandr
Junior Poster in Training
73 posts since May 2009
Reputation Points: 15
Solved Threads: 10
clienthint.js
function stateChanged() {
if (xmlhttp.readyState == 4) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
This is your stateChanged-function. What I suggest is that you replace it's content (inside the if) with
evalScript(xmlhttp.responseText, document.getElementById('txtHint'));
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);
}
What this function does is to strip the html of script-tags, then it takes the leftover html and insert it into div, and last it evaluates (run) the 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?
var script = ''; // or var scripts = ''; ?
Variable-names has no effect on the execution of JavaScript as long as they're named legally.
arguments[1] // what is it ?
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
Alxandr
Junior Poster in Training
73 posts since May 2009
Reputation Points: 15
Solved Threads: 10
debug: alert('scripts : ' + '\n' + scripts);
scripts :
<script>
function myfunc1() {
alert("button click");
}
</script>
The error lies here. The script-tags should be gone, only the content should be left. When you run eval on you're script-variable it'll fail, therefore myfunc1 is never created. But I can't find the error in your code...
Just to make shure I didn't mistype something; here is a direct copy of working code:
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
Alxandr
Junior Poster in Training
73 posts since May 2009
Reputation Points: 15
Solved Threads: 10
sorry Alxandr, it is my mistake. actually, the script-tags already gone.
debug: alert('scripts : ' + '\n' + scripts);
scripts :
function myfunc1() {
alert("button click");
}
However, the same test flow, the same result:
1. Click on 'Click to Alert'
2. Not work -> Error Console ( Error: myfunc1 is not defined )
Try changing line 2 to window.myfunc1 = function() {
Alxandr
Junior Poster in Training
73 posts since May 2009
Reputation Points: 15
Solved Threads: 10