ajax & php & javascript

I

Ivan P

Hello!

I have a index.php that on one click calls via AJAX a file.php.

index.php looks like this:

<html >
<head>

<script language="javascript" type="text/javascript" >


var http; // We create the HTTP Object
var dest;
var prenosi;
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}

function processStateChange(){
if (http.readyState == 4){
contentDiv = document.getElementById(dest);
if ((http.status == 200)||(http.status==0)){
response = http.responseText;
contentDiv.innerHTML = response;
} else {
contentDiv.innerHTML = "Error: Status "+http.status;
}
}
}



function loadHTML(URL, destination){
dest = destination;
var url;
http = getHTTPObject();
if(http){
http.onreadystatechange = processStateChange;
http.open("GET", URL, true);
http.send(null);
}
}

</head>
<body>

<span onclick="loadHTML(file.php','')">Click</span><br>
</html>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -

file.php looks like this:

<span id="countdowncontainer">
write something down
<script type="text/javascript">
document.write("1")
</script>
</span>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -

The problem is that "write something down" displays on the Internet
Explorer, but number 1 doesn't. If we call just file.php - it works then -
it displays both. Why is javascript not executing from ajax called
procedure?

Ivan Petrovic
 
R

Randy Webb

Ivan P said the following on 5/25/2006 5:55 AM:

function processStateChange(){

contentDiv.innerHTML = response;

file.php looks like this:

<span id="countdowncontainer">
write something down
<script type="text/javascript">
document.write("1")
</script>
</span>

The problem is that "write something down" displays on the Internet
Explorer, but number 1 doesn't.

It won't in most other browsers as well.
If we call just file.php - it works then - it displays both.
Yes.

Why is javascript not executing from ajax called procedure?

It has nothing to do with it being an "ajax called procedure". It has to
do with inserting the script via innerHTML and the script blocks don't
get processed when doing that.

If you want to execute script blocks, then you will have to use a
different method of inserting it in the page to get the browser - any
browser - to execute the script.

The only browser I recall that executed script's inserted via innerHTML
was NS6.
 
I

Ivan P

Thanks!

What are other methods than innerHTML that support processing JavaScripts on
callback? ... so I can search a web for examples...

Ivan
 
R

Randy Webb

Ivan P said the following on 5/25/2006 9:48 AM:
Thanks!

What are other methods than innerHTML that support processing JavaScripts on
callback? ... so I can search a web for examples...

createElement, appendChild.

Dynamically loading a JS file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top