469,898 Members | 1,863 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

var

function shownumber(val) {
// XBM format
var Head = "#define count_width 8\n"+
"#define count_height 16\n"+
"static char count_bits[] = {";
var Count, dig, tag;

Digit = new Array();
Digit[0]=Head+"0xff,0xff,0xff,0xc3,0x99,0x99,0x99,0x99,"+
"0x99,0x99,0x99,0x99,0xc3,0xff,0xff,0xff};";
// ... sniped ....
for (Count = 0; Count < val.length; Count++) {
dig=val.substring(Count, Count+1);
tag="<IMG SRC='javascript:Digit["+dig+"]' "+
"HEIGHT=16 WIDTH=8 ALIGN='absmiddle'>";
document.write(tag);
}
}

When I run shownumber("0"), an XBM would be disaplayed. But if I changed
the declaration for the array to:

var Digit = new Array();

shownumber() would no longer work. Why? Everything is contained in the
function. Why can't I use local variable for the array?

--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 12:08am up 1 day 28 min load average: 1.00 1.00 1.00
Jul 20 '05 #1
9 1258
> tag="<IMG SRC='javascript:Digit["+dig+"]' "+
"HEIGHT=16 WIDTH=8 ALIGN='absmiddle'>";
document.write(tag);
var Digit = new Array();
shownumber() would no longer work. Why? Everything is contained in the
function. Why can't I use local variable for the array?


I think I knew the answer. Something to do with order of execution.

What's the proper style for declaring global variable in Javascript?

--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 12:14am up 1 day 34 min load average: 1.01 1.01 1.00
Jul 20 '05 #2
> I think I knew the answer. Something to do with order of execution.

What's the proper style for declaring global variable in Javascript?


Generally, it is best to avoid global variables. When you cannot avoid
them, they should be defined before they are used.

http://www.crockford.com/#javascript
Jul 20 '05 #3
Douglas Crockford wrote:
I think I knew the answer. Something to do with order of execution.

What's the proper style for declaring global variable in Javascript?


Generally, it is best to avoid global variables. When you cannot avoid
them, they should be defined before they are used.

http://www.crockford.com/#javascript


you meant something like this?

<html><head>
<script type="text/javascript">
$global_var1 = new Array();
$global_var2 = 1;
$global_var3 = "";

Function one() {}
function two() {}

// main
window.onload = one;
</script>
</head><body>
</body></html>

--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 12:52am up 1 day 1:12 load average: 0.99 0.97 0.97
Jul 20 '05 #4
toylet <toylet_at_mail.hongkong.com> writes:
you meant something like this?
(Missing DOCTYPE declration, required by HTML) <html><head> (Missing title element, also required) <script type="text/javascript">
$global_var1 = new Array();


I would write

var $global_var1 = new Array();

Using the "var" declaration at the top level (not inside a function body)
declares a global variable.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
>>> What's the proper style for declaring global variable in Javascript?
Generally, it is best to avoid global variables. When you cannot avoid
them, they should be defined before they are used.
you meant something like this? <html><head>
<script type="text/javascript">
$global_var1 = new Array();
$global_var2 = 1;
$global_var3 = "";

Function one() {}
function two() {}

// main
window.onload = one;
</script>
</head><body>
</body></html>


Close. Leave off the type="text/javascript". That is a Microsoftism that
didn't make it into the standard.

I would write

var global_var1 = [];
var global_var2 = 1;
var global_var3 = "";

Use var to distinguish a new definition from using an existing
definition. Avoid using '$'. It was intended for use by program
generators. It looks ugly. Also, I prefer [] to new Array() .
Watch the capitalization on 'function'. JavaScript is case sensitive.
I would define function one this way:

window.onload = function one() {};

Finally, I recommend that you run your stuff through JSLint.

http://www.crockford.com/javascript/lint.html
Jul 20 '05 #6
> (Missing DOCTYPE declration, required by HTML)
(Missing title element, also required)


Not related to javascript, it seems that these 2 tags are not sondier
important by most browsers. At least, the page could still be rendered.
--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 1:28am up 1 day 1:48 load average: 1.00 1.00 1.00
Jul 20 '05 #7
> Close. Leave off the type="text/javascript". That is a Microsoftism that
didn't make it into the standard.
what should it be? `lanaguage="javascript"`? Thanks.
definition. Avoid using '$'. It was intended for use by program
Sorry, I mixed things up with php and perl.
Finally, I recommend that you run your stuff through JSLint.

http://www.crockford.com/javascript/lint.html


will try.
--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 1:36am up 1 day 1:56 load average: 0.99 0.94 0.95
Jul 20 '05 #8
toylet <toylet_at_mail.hongkong.com> writes:

[about <script type="text/javascript">]
Close. Leave off the type="text/javascript". That is a Microsoftism
that didn't make it into the standard.


what should it be? `lanaguage="javascript"`? Thanks.


You should use 'type="text/javascript"'. I think Richard managed to
misread it somehow. It is not related to Microsoft at all, but comes
from the HTML specification.

The type attribute is required by HTML 4, and the content type
"text/javascript" is the suggested value to use for Javascript content
(even though it is not standardized as a MIME type).

<URL:http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.1>

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #9
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
I think Richard


Make that Douglas. So much for short term memeory (I remembered two
newsgroups later, though :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by William C. White | last post: by
2 posts views Thread by Albert Ahtenberg | last post: by
3 posts views Thread by James | last post: by
reply views Thread by Ollivier Robert | last post: by
1 post views Thread by Richard Galli | last post: by
4 posts views Thread by Albert Ahtenberg | last post: by
1 post views Thread by inderjit S Gabrie | last post: by
2 posts views Thread by Jack | last post: by
3 posts views Thread by Sandwick | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.