| View previous topic :: View next topic |
| Author |
Message |
Drostin77
Joined: 27 Apr 2007 Posts: 2
|
Posted: Fri Apr 27, 2007 2:06 am Post subject: Simple question about function scope |
|
|
So i am developing in Coldfusion and asynchronous javascript . If i define a javascript function in my topmost page (the one in the url) any part of the page (a div who's inner html has been populated by AJAC) can see that function. However, if one of these divs which has been populated by AJAC tries to define its own function, by having the following in its html
<script type="text/javascript">
function foobar() {
window.alert("Hello World");
}
</script>
and call that function via an onclick (or any other event) i get
Error: foobar undefined.
I have tried giving the script an id, and appending it as a child to the documents Head, i have tried appending its text to an existing script in the documents head who's other functions work, foobar remains undefined. There is probably a simple fix, i probably phrased this poorly, please help if you can. |
|
| Back to top |
|
 |
|
|
LucasZ21 100+ Club

Joined: 26 Feb 2007 Posts: 195 Location: Mansfield, OH
|
Posted: Wed May 02, 2007 3:33 am Post subject: Re: Simple question about function scope |
|
|
You don't really need a function in javascript to make an alert pop up.
| Code: |
<html>
<head>
<title></title>
</head>
<body>
<form>
<input type="button" onclick="alert('Hello World')" value="Hello World">
</form>
</body>
</html>
|
I don't know why your function would be comming up as undefined though. Hope I helped. |
|
| Back to top |
|
 |
Drostin77
Joined: 27 Apr 2007 Posts: 2
|
Posted: Wed May 02, 2007 3:36 am Post subject: Re: Simple question about function scope |
|
|
Heh ya, i know i can do that. The alert is just to notify me that the function has even been called, the function actually needs to be a dynamically generated function each time that ajax page is loaded that populates drop downs with query data from a query who's parameters are dependant on the options they have chosen on the page before loading this div's ajax (i.e. from a form that is "submitted" via ajax (not an actual submit of course)). Thats why the code HAS to be in the page loaded via ajax.
Thank you for the answer though! your the first on like 4 forums to have answered, i think i phrased my question poorly. |
|
| Back to top |
|
 |
LucasZ21 100+ Club

Joined: 26 Feb 2007 Posts: 195 Location: Mansfield, OH
|
Posted: Wed May 02, 2007 4:55 pm Post subject: Re: Simple question about function scope |
|
|
| Drostin77 wrote: |
Heh ya, i know i can do that. The alert is just to notify me that the function has even been called, the function actually needs to be a dynamically generated function each time that ajax page is loaded that populates drop downs with query data from a query who's parameters are dependant on the options they have chosen on the page before loading this div's ajax (i.e. from a form that is "submitted" via ajax (not an actual submit of course)). Thats why the code HAS to be in the page loaded via ajax.
Thank you for the answer though! your the first on like 4 forums to have answered, i think i phrased my question poorly. |
I thought you were just having trouble getting a messege to appear haha.
It might just be the way you wrote it...
| Code: |
<script type="text/javascript">
function foobar() {
window.alert("Hello World");
}
</script>
|
I don't know if this matters but, when you declare a function, isn't there supposed to be a = sign and quotes around it?
Ex.
| Code: |
| function = "foobar()" |
Other than that I don't know. |
|
| Back to top |
|
 |
|