Javascript not running when called from an Ajax script

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Javascript not running when called from an Ajax script

Post by impulse() »

I have a page with an Ajax script included which retrieves the contents of another page every 10 seconds and displays the contents. In the page that is retrieved I have a bit of Javascript, as a test I've just used the alert() function and also document.write. But none are being run. It doesn't work in IE nor Firefox and the Firefox error console gives no errors at all.

The Ajax script it:

Code: Select all

function ajaxFunction(){
        var ajaxRequest;

        try{
                       ajaxRequest = new XMLHttpRequest();
        } catch (e){
        try{
                        ajaxRequest = new

ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try{
                                ajaxRequest = new

ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e){
                                alert("Your browser broke!");
                                return false;
                        }
                }
        }

        ajaxRequest.onreadystatechange = function(){
                if(ajaxRequest.readyState == 4){
                  document.getElementById('myDiv').innerHTML = ajaxRequest.responseText;
                }
        }
        ajaxRequest.open("GET", "http://my.page/page.php", true);
        ajaxRequest.send(null);
}
And page.php contains:

Code: Select all

<body>
<script type = 'text/javascript'>
  alert('Ello')
</script>

<?php

echo time();
echo "<script> alert('boo') </script>";
?>
If I go directly to page.php I get both the popups, but they are not used when called from the Ajax script.

Any ideas why this is?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

the global javascript is only run when the page is loaded. After that you can add javascript to a page, but you have to call it manually if you do.

An alternative would be to use jQuery's $.getScript(url, callback) method, but it expects pure javascript - not mixed content.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

You and your jQuery :D

I'll give it a blast.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I predict another convert. I'll have NSG order you up a jumpsuit :wink:
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Hehe.

This method wont work anyway, I need to pass in PHP data as function parameters to JS.

I had a script a few months ago that worked on a 10 second timer and retrieved JS but I'll be dammed if I can find it.
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

Personally I use Prototype.

This will do what you want, set evalScripts in the options and it will run any script tags in the return.
http://www.prototypejs.org/api/ajax/periodicalUpdater
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I've just remembered how I did it. I used an iframe and used a onLoad() event for the iFrame.


DOH!

:D
Post Reply