By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

calling functions from cgi

P: n/a
If I've loaded a set of functions in a webdocument like this:
<script language="JavaScript" type="text/javascript"
src="/js/foo.js"</script>

Shouldn't I be able to call a function from inside foo.js later in that
same document by doing something like this:

<script language="JavaScript" type="text/javascript">myfunction(myargs)
</script>

Even though it's generated from a CGI script? The all loaded javascript
files have the same scope do they not? It's not like the functions in
foo.js available only to other functions within that file? Right?
Jan 30 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Tony Rice wrote:
If I've loaded a set of functions in a webdocument like this:
<script language="JavaScript" type="text/javascript"
Language is deprecated, drop it.

src="/js/foo.js"</script>

Shouldn't I be able to call a function from inside foo.js later in that
same document by doing something like this:

<script language="JavaScript" type="text/javascript">myfunction(myargs)
</script>
Yes.
Even though it's generated from a CGI script? The all loaded javascript
What is generated from CGI - the script or the HTML document?

files have the same scope do they not? It's not like the functions in
foo.js available only to other functions within that file? Right?


Yes.

Do you get errors? What is the error message? Is the error because the
file isn't loaded, or because you are trying to manipulate objects in
the page before it's full loaded?
--
Rob
Jan 30 '06 #2

P: n/a
Tony Rice wrote:
If I've loaded a set of functions in a webdocument like this:
<script language="JavaScript" type="text/javascript"
src="/js/foo.js"</script>
The language attribute is deprecated. You seem to have forgotten the ">"
from the opening script tag.
Shouldn't I be able to call a function from inside foo.js later in that
same document by doing something like this:

<script language="JavaScript" type="text/javascript">myfunction(myargs)
</script>
Yes.
Even though it's generated from a CGI script?
How the server works out what to send to the user isn't relevant.
The all loaded javascript files have the same scope do they not? It's not
like the functions in foo.js available only to other functions within that
file? Right?


Right.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jan 30 '06 #3

P: n/a
Tony Rice wrote:
If I've loaded a set of functions in a webdocument like this:
<script language="JavaScript" type="text/javascript"
src="/js/foo.js"</script>

Shouldn't I be able to call a function from inside foo.js later in that
same document by doing something like this:
No, you need a ">" after: src="/js/foo.js"
Mick


<script language="JavaScript" type="text/javascript">myfunction(myargs)
</script>

Even though it's generated from a CGI script? The all loaded javascript
files have the same scope do they not? It's not like the functions in
foo.js available only to other functions within that file? Right?

Jan 30 '06 #4

P: n/a
Tony Rice said the following on 1/30/2006 6:11 PM:
If I've loaded a set of functions in a webdocument like this:
<script language="JavaScript" type="text/javascript"
src="/js/foo.js"</script>
Drop the language attribute though.
Shouldn't I be able to call a function from inside foo.js later in that
same document by doing something like this:

<script language="JavaScript" type="text/javascript">myfunction(myargs)
</script>
Yes, that is exactly how you would do it, but drop the language attribute.
Even though it's generated from a CGI script?
The script engine in the browser doesn't know, nor does it care, what
generated the script contents.

The all loaded javascript files have the same scope do they not?
They have the same scope as if you manually included them in the page,
which is what the browser does.

<script type="text/javascript" src="myJSFile.js"></script>

And if myJSFile.js had a function in it like this:

function myFunction(){
alert('This is an alert');
}

That function will have the same "scope" as if you did this:

<script type="text/javascript">
function myFunction(){
alert('This is an alert');
}
</script>

It's not like the functions in foo.js available only to other
functions within that file? Right?


Correct.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 31 '06 #5

P: n/a
RobG <rg***@iinet.net.au> wrote in news:pkxDf.241$Nv4.45834
@news.optus.net.au:
Do you get errors? What is the error message? Is the error because the
file isn't loaded, or because you are trying to manipulate objects in
the page before it's full loaded?


The errors I'm getting are DOM related. Wanted to make sure I was on the
right track before I dug anymore.

I'm trying to maintain state in a URL for a AJAX app. Make a element on
the page loadable via an ajax call or load automatically based on arguments
passed in the URL. But I'm getting DOM errors when I call the function
inline that is normally called from a user click.
Jan 31 '06 #6

P: n/a
Tony Rice wrote:
RobG <rg***@iinet.net.au> wrote in news:pkxDf.241$Nv4.45834
@news.optus.net.au:

Do you get errors? What is the error message? Is the error because the
file isn't loaded, or because you are trying to manipulate objects in
the page before it's full loaded?

The errors I'm getting are DOM related. Wanted to make sure I was on the
right track before I dug anymore.

I'm trying to maintain state in a URL for a AJAX app. Make a element on
the page loadable via an ajax call or load automatically based on arguments
passed in the URL. But I'm getting DOM errors when I call the function
inline that is normally called from a user click.


That sounds like you are trying to access elements before the page has
created them, either onload or after the AJAX stuff.

Code snippet? URL? Did you fix the missing '>' from the closing script
tag noted in other responses?

--
Rob
Jan 31 '06 #7

P: n/a
RobG <rg***@iinet.net.au> wrote in news:zvBDf.251$Nv4.46029
@news.optus.net.au:
That sounds like you are trying to access elements before the page has
created them, either onload or after the AJAX stuff.

Code snippet? URL?
I'm using jah (http://microformats.org/wiki/rest/ahah) to load URLs inline
dynamically. The perl script looks like this:

my $ajaxcall="jah('myotherscript.cgi?myarg=arg', 'contentdiv')";
if (param('myparam')){
print "<script type=\"text/javascript\">$ajaxcall</script>\n";

}else{
print a({href=>"javascript:$ajaxcall"}, 'click to load');
}
print "<div id='contentdiv'></div>\n";

The goal is to have the content loaded inline automatically if the
'myparam' is passed in the query string and make it loadable inline by
clicking the link.
I'm getting the following errors:

document.getElementById(target) has no properties

and

href has no properties
These are both coming from the ahah code. What I dont get is what is
special about the click. Either way it's accessing the document object so
it's not like the click is providing an object.

Any ideas?
Did you fix the missing '>' from the closing script
tag noted in other responses?


yes. That was a cut and paste error between the code and my newsreader.
Jan 31 '06 #8

P: n/a
Tony Rice wrote:
RobG <rg***@iinet.net.au> wrote in news:zvBDf.251$Nv4.46029
@news.optus.net.au:

That sounds like you are trying to access elements before the page has
created them, either onload or after the AJAX stuff.

Code snippet? URL?

I'm using jah (http://microformats.org/wiki/rest/ahah) to load URLs inline
dynamically. The perl script looks like this:

my $ajaxcall="jah('myotherscript.cgi?myarg=arg', 'contentdiv')";
if (param('myparam')){
print "<script type=\"text/javascript\">$ajaxcall</script>\n";

}else{
print a({href=>"javascript:$ajaxcall"}, 'click to load');
}
print "<div id='contentdiv'></div>\n";


Don't post server code, post what your client gets - this is a
JavaScript news group, not Perl :-)

What content is written to the script element? What is written as the
value of the A element's href attribute? What is the surrounding HTML?

I have no idea what is ending up in your A element attributes, so it's
hard to help. Use view source on the client to get the actual HTML that
is delivered to your page (a minimal test case will do).

The goal is to have the content loaded inline automatically if the
'myparam' is passed in the query string and make it loadable inline by
clicking the link.
I'm getting the following errors:

document.getElementById(target) has no properties
The value of 'target' should be the ID of an element on your page. If
the page hasn't finished loading (unlikely), or the ID doesn't exist
(much more likely), then you will get the error you get.

The 'ahah' code does not test to see if features are supported, nor does
it check to see if document.getElementById(target) returns anything
useful before trying to access its innerHTML property.


and

href has no properties

These are both coming from the ahah code. What I dont get is what is
special about the click. Either way it's accessing the document object so
it's not like the click is providing an object.


The click runs the script that is in the A element's href attribute. If
you show the client HTML, we can see what is actually in the href attribute.

--
Rob
Feb 1 '06 #9

P: n/a
RobG <rg***@iinet.net.au> wrote in
news:qG*****************@news.optus.net.au:
Tony Rice wrote:
RobG <rg***@iinet.net.au> wrote in news:zvBDf.251$Nv4.46029
@news.optus.net.au:

That sounds like you are trying to access elements before the page
has created them, either onload or after the AJAX stuff.

Code snippet? URL?

I'm using jah (http://microformats.org/wiki/rest/ahah) to load URLs
inline dynamically. The perl script looks like this:

my $ajaxcall="jah('myotherscript.cgi?myarg=arg', 'contentdiv')";
if (param('myparam')){
print "<script type=\"text/javascript\">$ajaxcall</script>\n";

}else{
print a({href=>"javascript:$ajaxcall"}, 'click to load');
}
print "<div id='contentdiv'></div>\n";


Don't post server code, post what your client gets - this is a
JavaScript news group, not Perl :-)

What content is written to the script element? What is written as the
value of the A element's href attribute? What is the surrounding
HTML?

I have no idea what is ending up in your A element attributes, so it's
hard to help. Use view source on the client to get the actual HTML
that is delivered to your page (a minimal test case will do).


Sorry, thought it was obvious from that code snippet and it's important
to show how the javascript is generated differently depending on the
context.

The problem has been fixed by putting

<A HREF="javascript:jah('something.cgi', 'contentdiv')>click to load</A>

inside <div id='contentdiv'> rather than outside.

I have no idea why this fixed it. This is the only change made but now
I'm getting no further errors from getElementById when calling the ahah
function directly.
The goal is to have the content loaded inline automatically if the
'myparam' is passed in the query string and make it loadable inline
by clicking the link.
I'm getting the following errors:

document.getElementById(target) has no properties


The value of 'target' should be the ID of an element on your page. If
the page hasn't finished loading (unlikely), or the ID doesn't exist
(much more likely), then you will get the error you get.


I checked that angle out as well. didn't matter where the target div was
placed in relation ship to the ahah call.

Not sure why it works not, but at least it works.
Feb 1 '06 #10

P: n/a
Tony Rice wrote:
RobG <rg***@iinet.net.au> wrote in
news:qG*****************@news.optus.net.au:

Tony Rice wrote:
RobG <rg***@iinet.net.au> wrote in news:zvBDf.251$Nv4.46029
@news.optus.net.au:

That sounds like you are trying to access elements before the page
has created them, either onload or after the AJAX stuff.

Code snippet? URL?
I'm using jah (http://microformats.org/wiki/rest/ahah) to load URLs
inline dynamically. The perl script looks like this:

my $ajaxcall="jah('myotherscript.cgi?myarg=arg', 'contentdiv')";
if (param('myparam')){
print "<script type=\"text/javascript\">$ajaxcall</script>\n";

}else{
print a({href=>"javascript:$ajaxcall"}, 'click to load');
}
print "<div id='contentdiv'></div>\n";
Don't post server code, post what your client gets - this is a
JavaScript news group, not Perl :-)

What content is written to the script element? What is written as the
value of the A element's href attribute? What is the surrounding
HTML?

I have no idea what is ending up in your A element attributes, so it's
hard to help. Use view source on the client to get the actual HTML
that is delivered to your page (a minimal test case will do).

Sorry, thought it was obvious from that code snippet and it's important
to show how the javascript is generated differently depending on the
context.


It's never safe to assume what the client gets from the server based on
server code. Only by posting what the client gets can the code be
debugged with any certainty.

Use view source, or one of the 'view innerHTML' bookmarklets, e.g. copy
and paste the following as a singe line in the address:

javascript:'<code>'+(document.documentElement ||
document.body).innerHTML.replace(/&/g,%22&amp;%22).replace(/</g,
%22&lt;%22).replace(/%20%20/g, %22&nbsp;%20%22).replace(/\n/g,
%22<br>%22)+'<\/code>';

The problem has been fixed by putting

<A HREF="javascript:jah('something.cgi', 'contentdiv')>click to load</A>

inside <div id='contentdiv'> rather than outside.

I have no idea why this fixed it. This is the only change made but now
I'm getting no further errors from getElementById when calling the ahah
function directly.
There is no ahah function, there is jah() and jahDone() (which is called
by jah()).

Until you post the page source that the client gets, is anyone's guess
as to why it now 'works'. jahDone() should replace the content of
'contentdiv' with whatever is sent back from the server, so your link
will be replaced.
[...]
I'm getting the following errors:

document.getElementById(target) has no properties


The value of 'target' should be the ID of an element on your page. If
the page hasn't finished loading (unlikely), or the ID doesn't exist
(much more likely), then you will get the error you get.


I checked that angle out as well. didn't matter where the target div was
placed in relation ship to the ahah call.


The ahah code runs from a click (as far as I can tell) therefore the
page /should/ be loaded and contentdiv should be able to be anywhere in
the page.

Not sure why it works not, but at least it works.


Unless you post the page source actually received by the client...
--
Rob
Feb 1 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.