Can't Return any values from Functions

Z

Zhichun Pu

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
 
G

Grant Wagner

Zhichun said:
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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 
Z

Zhichun Pu

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top