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

Can't Return any values from Functions

P: n/a
Hi guys,

I can't seem to get anything returned from functions.

If I use the <script src = ... > command, nothing works. But if I
define the functions locally within the file, then it works.

Something simple:

function abc() { return 99; }

If I tried to access this function by placing it inside a .js file,
and then including it, it returns "undefined". Everything else works
except for returned values...

If I define this function locally, it returns correctly the value of
99.

What is going on?? This is so weird.

Zhichun
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Zhichun Pu wrote:
Hi guys,

I can't seem to get anything returned from functions.

If I use the <script src = ... > command, nothing works. But if I
define the functions locally within the file, then it works.

Something simple:

function abc() { return 99; }

If I tried to access this function by placing it inside a .js file,
and then including it, it returns "undefined". Everything else works
except for returned values...

If I define this function locally, it returns correctly the value of
99.

What is going on?? This is so weird.

Zhichun


Without more information it's hard to know what the problem is. However,
one mistake some people make is to include <script></script> tags in
their external .js file. The .js file should only contain JavaScript, it
should contain no HTML tags or comments.

So if your inline code is:

<script type="text/javascript">
<!--
function abc() { return 99; }
//-->
</script>

Then the external .js file should contain only:

function abc() { return 99; }

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #2

P: n/a
I tried writing a few more test scripts and they all worked.... maybe
it was the script I was using that didn't work.

function alertSize() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else {
if( document.documentElement &&
( document.documentElement.clientWidth ||
document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else {
if( document.body && ( document.body.clientWidth ||
document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
}
}
window.alert( 'Width = ' + myWidth );
window.alert( 'Height = ' + myHeight );
}

I pulled it off a website. It's supposed to get the width and height
of a browser's window. I just did "return [myWidth, myHeight];" and it
didn't work. I tried to return a single value and that didn't work
either.

Maybe it also had to do with the DynAPI package I was using?
Javascript has changed so much since I last programmed in it....

Any comments + ideas would be greatly appreciated. Thanks to all those
who tried to help =)

Zhichun

sc*******@hotmail.com (Zhichun Pu) wrote in message news:<e8*************************@posting.google.c om>...
Hi guys,

I can't seem to get anything returned from functions.

If I use the <script src = ... > command, nothing works. But if I
define the functions locally within the file, then it works.

Something simple:

function abc() { return 99; }

If I tried to access this function by placing it inside a .js file,
and then including it, it returns "undefined". Everything else works
except for returned values...

If I define this function locally, it returns correctly the value of
99.

What is going on?? This is so weird.

Zhichun

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.