PDA

View Full Version : Insert javascript using ajax



The Dukestir
May 22nd, 2007, 06:24 AM
I'm working on a project that needs the ability to insert javascript functions and code after the page is fully loaded. The content of the page is going to change dynamically as it is updated through ajax, and I need to be able to add javascript to manipulate new content on the page. For instance, I want to be able to add a button to the screen, and when I add that, add a javascript function to handle when the button is clicked. I have tried just adding it to the body of the document, but I still get an error saying that the function does not exist. I am using Firefox 2.0, which is the only browser it will ever need to run on, so firefox only fixes are ok.

glenngv
May 22nd, 2007, 06:17 PM
Why not just attach an onclick event in the button?

button.onclick = function(){
alert('click');
}

The Dukestir
May 23rd, 2007, 09:58 PM
That would work for the buttons, but what if I need functions to be available to other processes already running on screen. For instance, if I wanted some recursive function added in later, there is no way to call it. I have tried adding in code by using eval on the full function name() {...}, but that doesn't seem to pull it in. I have added it in to its own script section as well. I have gotten everything working, but it is really messy and others wouldn't be able to add on later, so I'm trying to come up with a way to clean up the code.

Thanks.

The Dukestir
May 24th, 2007, 12:19 AM
I found a way to get it to work. I just made an iframe with 0 height and width. I then load in the javascript to that frame and access it through the frame.

glenngv
May 24th, 2007, 04:11 PM
You could also do this:

function addScript(jsFile){
var js = document.createElement("script");
js.setAttribute("type", "text/javascript");
js.setAttribute("src", jsFile);
document.getElementsByTagName("head")[0].appendChild(js);
}
...
addScript("js/sample.js");

felgall
May 24th, 2007, 10:03 PM
JavaScript and images can be downloaded and added to a web page at any time. This has been true for as long as web pages have supported JavaScript. No "Ajax" or "iframe" processing is required as they are the FOUR main methods of retrieving information from the server without a page reload.

p9939068
Jun 20th, 2007, 12:21 AM
I am facing the exact same problem, but don't quite understand the iframe method as described.

Let me elaborate. I'm using PHP together with AJAX to output dynamic content. Part of this content is a piece of dynamic javascript code which is supposed to load together with the dynamic content, but we all know now that javascript snippets loaded dynamically through AJAX will not be executed. Any kind sirs with a good idea on how to go about this?

glenngv
Jun 20th, 2007, 01:55 AM
Can you post a sample of the dynamic content?

rnd me
Jun 21st, 2007, 09:30 PM
we all know now that javascript snippets loaded dynamically through AJAX will not be executed.

//ex:
function ajCallBack(resp){ eval( resp ) }; //for code/functions

function ajCallBack(resp){ eval( " ( "+ resp +" ) " ) }; //for JSON anon Objects