Forum Moderators: open

Message Too Old, No Replies

Calling a script from within a javascript function

         

tim222

10:05 pm on Sep 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a way a way to run a script from within a javascript function?

I already know how to do this while the page is loading. In a nutshell, I'm doing a conditional test that decides which script I want to run. For example:


<script type='text/javascript'>

if (x == 1){
include_dom('script.php?x=1');
} else {
include_dom('script.php?x=2');
}

function include_dom(script_filename) {
var html_doc = document.getElementsByTagName('head').item(0);
var js = document.createElement('script');
js.setAttribute('language', 'javascript');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', script_filename);
html_doc.appendChild(js);
return false;
}

</script>

This works great while the page is loading. Now I want to expand on this and run a script in response to an onClick event. For example:


<DIV onClick='DivClicked()'>
some HTML content
</DIV>

<script type='text/javascript'>
function DivClicked(){
include_dom('script.php?x=3');
}
</script>

The problem is that when the DIV element is clicked, the call to include_dom() seems to be ignored. Either it's not executing the code, or there's a problem during execution, or I don't know what. It's probably not a problem with the PHP script, because I would see errors in the error log and there are none. The browser doesn't report any Javascript errors.

The only thing I can think of is that somehow the code only works while the page is loading, but even that is just a guess.

Does anybody have some insight on this?

Thanks

misterjonez

6:16 am on Sep 18, 2007 (gmt 0)

10+ Year Member



ajax is your friend dude!

[prototypejs.org...]