473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Safari and JavaScript arrays

Hello,

I am querying the JavaScript community to learn if
others are having problems with Safari[1] and JavaScript
arrays:

myImages = [
"images/image0.jpg",
"images/image1.jpg",
"images/image2.jpg",
];

then referenced like this:

document.images[1].src = myImages[1];

Is this a known bug with Safari? Broken arrays?

(BTW the above works in every major browser's
recent version; those I have tried)

I can write a string literal into the src property:

document.images[1].src = "images/image1.jpg";

and achieve image replacement, but not when the string
literal is referenced from an array.

What gives? Am I doing something Really Stupid (TM)?

Thanks in advance for advice.

Cheers,
DK

[1] Referring to Safari version 1.0.1
Jul 23 '05 #1
12 2506
> I am querying the JavaScript community to learn if
others are having problems with Safari[1] and JavaScript
arrays:

myImages = [
"images/image0.jpg",
"images/image1.jpg",
"images/image2.jpg",
];
Hmmm. There's line breaks in there. Try it on one line.

Near as I can tell tell Safari handles arrays the same as othres. At least I
haven't seen problems.

Jeff


then referenced like this:

document.images[1].src = myImages[1];

Is this a known bug with Safari? Broken arrays?

(BTW the above works in every major browser's
recent version; those I have tried)

I can write a string literal into the src property:

document.images[1].src = "images/image1.jpg";

and achieve image replacement, but not when the string
literal is referenced from an array.

What gives? Am I doing something Really Stupid (TM)?

Thanks in advance for advice.

Cheers,
DK

[1] Referring to Safari version 1.0.1

Jul 23 '05 #2
This was a weird one...

Here's the bottom line:

Be careful to not use the same names in varible namespace and
selector namespace on Safari version <= 1.2.1 (125.1).

I was doing something like this:

document.getElementById("myImage").src = myImage[index];

where I had an array of strings labelled "myImage" and
and id selector in the HTML called "myImage".

When the JavaScript array name was modifed so that it would
not clash with the id selector, Safari worked properly.

So, my way of fixing it was simply to the change my array name
to "myImages" array.

Cheers,
DK
Jul 23 '05 #3
Duderonomoy <Se*@reply.to> wrote in message news:<pa****************************@reply.to>...
where I had an array of strings labelled "myImage" and
and id selector in the HTML called "myImage".

When the JavaScript array name was modifed so that it would
not clash with the id selector, Safari worked properly.


Does it have the same behaviour as IE then. You can refer to a page
element in javascript by its id attribute value? For example if you
have

[xml id="xmlData"][/xml]

In javascript you can refer to it like this:

xmlData.src = '/xml/data.xml';

If so that'd be the cause of the clash.
Jul 23 '05 #4
On Sat, 24 Apr 2004 06:27:20 GMT, Duderonomoy <Se*@reply.to> wrote:

[snip]
I was doing something like this:

document.getElementById("myImage").src = myImage[index];

where I had an array of strings labelled "myImage" and
and id selector in the HTML called "myImage".

When the JavaScript array name was modifed so that it would
not clash with the id selector, Safari worked properly.

So, my way of fixing it was simply to the change my array name
to "myImages" array.


How did you declare the identifier? In IE, using the var keyword overrides
document globals for that scope (IDs and the like).

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #5
On Sun, 25 Apr 2004 00:20:27 +0000, Michael Winter wrote:
On Sat, 24 Apr 2004 06:27:20 GMT, Duderonomoy <Se*@reply.to> wrote:

[snip]
I was doing something like this:

document.getElementById("myImage").src = myImage[index];

where I had an array of strings labelled "myImage" and
and id selector in the HTML called "myImage".

When the JavaScript array name was modifed so that it would
not clash with the id selector, Safari worked properly.

So, my way of fixing it was simply to the change my array name
to "myImages" array.


How did you declare the identifier? In IE, using the var keyword overrides
document globals for that scope (IDs and the like).

Mike


Thanks for the reflective surface :)

Hmmm. I am not sure of the ramifications of what you are indicating.
Though I am very interested. I assumed such symbolic names would be
in different name spaces.

I was declaring data like this, in a separate file:

var demoText = [
"string one",
"string two",
"string thr",
]

var demoImage = [
"string/path/one.jpg",
"string/path/two.jpg",
"string/path/thr.jpg",
]

In another file an event handler:

function ClickHandler (evt)
{
<snip>
document.getElementById("demoImage").src = demoImage[idx];
document.getElementById("demoText").innerHTML = demoText[idx];
<snip>
}

And finally the HTML file:

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<script src="data.js"></script>
<script src="events.js"></script>
</head>

<body>
<img id="demoImage" src="">
<p id="demoText">blah</p>
</body>
</html>

Cheers,
DK

Jul 23 '05 #6
Duderonomy wrote:
<snip>
I was declaring data like this, in a separate file:

var demoText = [
"string one",
"string two",
"string thr", ^ ]

var demoImage = [
"string/path/one.jpg",
"string/path/two.jpg",
"string/path/thr.jpg", ^ ]

<snip>

A running theme in your Array literal definitions is a final Elison,
which is allowed but is known to be inconstantly handled across
browsers. ECMA 262 would require the resulting Array to be 4 elements
long with the first three elements containing strings and the fourth
element undefined. There doesn't seem to be any good reason for creating
Arrays that are longer than the number of string literals they contain
and any looping up to demoText.length, for example, may result in the
final undefined element being used (with potentially error-producing
results).

Richard.
Jul 23 '05 #7
"Richard Cornford" <Ri*****@litotes.demon.co.uk> writes:

[Array written ["a","b","c",]...]
A running theme in your Array literal definitions is a final Elison,
which is allowed but is known to be inconstantly handled across
browsers. ECMA 262 would require the resulting Array to be 4 elements
long with the first three elements containing strings and the fourth
element undefined.


No, ECMA 262 v3 (haven't checked earlier versions) would required the
array to have length 3.

The relevant rule of 11.1.4 is
ArrayLiteral : [ ElementList , Elision_opt ]
where ElementList matches the three strings separated by commas,
and Elision_opt is empty.

If you check the evaluation of this rule
---
The production ArrayLiteral : [ ElementList , Elisionopt ] is
evaluated as follows:
1. Evaluate ElementList.
2. Evaluate Elision; if not present, use the numeric value zero.
3. Call the [[Get]] method of Result(1) with argument "length".
4. Call the [[Put]] method of Result(1) with arguments "length" and
(Result(2)+Result(3)).
5. Return Result(1).
---
the ElementList evaluates to the array ["a","b","c"]. The rule for
ArrayLitreals increases the length of this array with the size of the
(empty) Elision (it specifically says to use zero if the elision is
omitted). It does not include the litreal comma of the rule in this
count.

I believe this rule is deliberate, attempting to mimic Java's
literal array notation, where a final comma is allowed.

However, IE 6 still fails to implement this correctly, and thinks
["a",].length == 2, so there is reason to omit the unnecessary comma.

/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 23 '05 #8
Lasse Reichstein Nielsen wrote:
"Richard Cornford" <Ri*****@litotes.demon.co.uk> writes:

[Array written ["a","b","c",]...]
A running theme in your Array literal definitions is a final Elison,
which is allowed but is known to be inconstantly handled across
browsers. ECMA 262 would require the resulting Array to be 4 elements
long with the first three elements containing strings and the fourth
element undefined.
No, ECMA 262 v3 (haven't checked earlier versions) would required the
array to have length 3.

The relevant rule of 11.1.4 is
ArrayLiteral : [ ElementList , Elision_opt ]
where ElementList matches the three strings separated by commas,
and Elision_opt is empty.

If you check the evaluation of this rule
---
The production ArrayLiteral : [ ElementList , Elisionopt ] is
evaluated as follows:
1. Evaluate ElementList.
2. Evaluate Elision; if not present, use the numeric value zero.
3. Call the [[Get]] method of Result(1) with argument "length".
4. Call the [[Put]] method of Result(1) with arguments "length" and
(Result(2)+Result(3)).
5. Return Result(1).
---
the ElementList evaluates to the array ["a","b","c"]. The rule for
ArrayLitreals increases the length of this array with the size of the
(empty) Elision (it specifically says to use zero if the elision is
omitted). It does not include the litreal comma of the rule in this
count.


It comes down to that separating comma in the ArrayLiteral production,
is it an Elison itself or just a separator of the ElementList from any
following optional Elison? Reviewing the production rules with the comma
in that rule highlighted I tend to agree with you that an isolated
trailing comma should not make the Array any longer than the
ElementList.

I seem to remember having this discussion with you before, your
conclusion didn't stick in my memory because I prefer to deal with the
issue by avoiding even attempting to defined sparse arrays, and never
include trailing commas.
I believe this rule is deliberate, attempting to mimic Java's
literal array notation, where a final comma is allowed.

However, IE 6 still fails to implement this correctly, and thinks
["a",].length == 2, so there is reason to omit the unnecessary comma.


There is no question that this is an area of inconsistent
implementations that is best avoided entirly.

Richard.
Jul 23 '05 #9
On Sun, 25 Apr 2004 14:40:12 +0100, Richard Cornford wrote:
Duderonomy wrote:
<snip>
I was declaring data like this, in a separate file:

var demoText = [
"string one",
"string two",
"string thr", ^
]

A running theme in your Array literal definitions is a final Elison,
which is allowed but is known to be inconstantly handled across
browsers. ECMA 262 would require the resulting Array to be 4 elements
long with the first three elements containing strings and the fourth
element undefined. There doesn't seem to be any good reason for creating
Arrays that are longer than the number of string literals they contain
and any looping up to demoText.length, for example, may result in the
final undefined element being used (with potentially error-producing
results).

Richard.


Thanks for that feedback. It prompted me to make two version of my data
file, both with and without the trailing Elision. After testing it is
clear that such an issue was not causing the problem. Regardless, I will
not be inserting the Elision at the end of such definitions any more... I
never did that any other languages; so why start now. :)

Also, my code does not iterate across the array... Hence I would not
encounter this issue of a potentially undefined value.

Ultimately, the index, idx, is generated by an 'id' selector value of a
unique menu item; in my case, a Handler is tied to the onClick property of
a series of LI boxes that serve as menu items. So, when a menu item box is
clicked, an integer index value is derived from a unique id selector value
specified as an attribute of every LI element.

Thanks for the excellent feedback everyone... :)
I certainly know more stuff than before I asked you folks about this...

I submitted this issue as a bug against Safari. I'll follow-up here with
Apple's response.

Cheers,
DK

Jul 23 '05 #10
On Sun, 25 Apr 2004 05:43:19 GMT, Duderonomy <Se*@reply.to> wrote:
On Sun, 25 Apr 2004 00:20:27 +0000, Michael Winter wrote:


[snip]
How did you declare the identifier? In IE, using the var keyword
overrides document globals for that scope (IDs and the like).


Thanks for the reflective surface :)

Hmmm. I am not sure of the ramifications of what you are indicating.
Though I am very interested. I assumed such symbolic names would be
in different name spaces.


No, they're simply globals. See (watch for wrap)

<URL:http://www.google.com/gr************************************************ ********************************************@news-text.blueyonder.co.uk&lr=&num=50&hl=en>

However, based on the code you posted, it isn't a factor.

[snip]

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #11
Jeff Thies wrote:
I am querying the JavaScript community to learn if
others are having problems with Safari[1] and JavaScript
arrays:

myImages = [
"images/image0.jpg",
"images/image1.jpg",
"images/image2.jpg",
];


Hmmm. There's line breaks in there. Try it on one line.


Name a reason why this could help.
PointedEars
Jul 23 '05 #12
Richard Cornford wrote:
Lasse Reichstein Nielsen wrote:
[Array written ["a","b","c",]...]

[...] ECMA 262 v3 (haven't checked earlier versions) would required
the array to have length 3.

The relevant rule of 11.1.4 is
ArrayLiteral : [ ElementList , Elision_opt ]
where ElementList matches the three strings separated by commas,
and Elision_opt is empty.

If you check the evaluation of this rule
---
The production ArrayLiteral : [ ElementList , Elisionopt ] is
evaluated as follows:
1. Evaluate ElementList.
2. Evaluate Elision; if not present, use the numeric value zero.
3. Call the [[Get]] method of Result(1) with argument "length".
4. Call the [[Put]] method of Result(1) with arguments "length" and
(Result(2)+Result(3)).
5. Return Result(1).
---
the ElementList evaluates to the array ["a","b","c"]. The rule for
ArrayLitreals increases the length of this array with the size of the
(empty) Elision (it specifically says to use zero if the elision is
omitted). It does not include the litreal comma of the rule in this
count.


It comes down to that separating comma in the ArrayLiteral production,
is it an Elison itself or just a separator of the ElementList from any
following optional Elison?


With

| 5.1.5 Grammar Notation
|
| [...]
| As another example, the syntactic definition:
|
| ArgumentList :
| AssignmentExpression
| ArgumentList , AssignmentExpression
|
| states that an ArgumentList may represent either a single
| AssignmentExpression or an ArgumentList, followed by a
| comma, followed by an AssignmentExpression.

and

| ArrayLiteral : [ ElementList , Elision_opt ]

[0, 1,]

means that

"[" == "["
"ElementList" == "0, 1"
"," == ","
"Elision(_opt)" == [not present]
"]" == "]"

(note that unlike Elision_opt_, that "," is _not_ optional!)

and thus

| 1. Evaluate ElementList.

# Result(1) := [0, 1]

| 2. Evaluate Elision; if not present, use the numeric value zero.

# Result(2) := [not present]
# Result(2) := 0

| 3. Call the [[Get]] method of Result(1) with argument "length".

# Result(3) := Result(1).[[Get]]("length")
# Result(3) := [0, 1].[[Get]]("length")
# Result(3) := 2

| 4. Call the [[Put]] method of Result(1) with arguments "length"
| and (Result(2)+Result(3)).

# Result(1).[[Put]]("length", Result(2)+Result(3))
# Result(1).[[Put]]("length", 0+2)
# Result(1).[[Put]]("length", 2)

| 5. Return Result(1).

# Return [0, 1].

So

[0, 1,] "==" [0, 1]

and

[0, 1,].length == 2. (see 4.)

But

| Elision :
| ,
| Elision ,

allows for

[0, 1,,]

where the last "," must be Elision_opt_ and thus

[0, 1,,] "==" [0, 1, 0,] "==" [0, 1, 0]

so

[0, 1,,].length == 3
[0, 1,,,].length == 4
...
HTH

PointedEars
Jul 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bernard | last post by:
Hi, I am suddenly getting Safari script errors with the following user agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.2 (KHTML, like Gecko) Safari/125.8 In a...
5
by: joaopedrogoncalves | last post by:
Hi, I want to load an external javascript file, get its results and stick them inside a <div> block. I also want to do this in several places on a web page. This way the browser doesn't have...
8
by: Giuseppe Chielli | last post by:
Hi to everyone? Can someone tell me if there is a way to identify Safari...I found out that navigator.appName returns "Netscape" and I didn't found any option to change the browser's definition......
4
by: Pasquale | last post by:
With the code below, can anyone see why the array and/or the field in the last line are empty for Safari users? It works with Netscape 6 and up, IE 6, Firefox, Konqueror. Thanks, Pasquale ...
2
by: laredotornado | last post by:
Hi, Is it possible to fool Javascript running on a Mac Safari web browser into believing it is a PC IE browser? We have the following JS code that is detecting both Mac and Safari. Sadly, we do...
3
by: Joe Cox | last post by:
I am having a problem with style properties for dynamic images in Mac OS X Safari. By dymanic images, I mean images allocated with the javascript 'new Image()' call. With static images (created...
2
by: vendredi5h | last post by:
Hello, Yesterday I spent a lot of time to find why my javascript script was not working on Safari. I finaly found a solution but not the reason. And I'd like to know if someone could tell me if...
7
by: Tom | last post by:
I have an oo-type javascript program running perfectly on IE 6.0+, FF 1.5+, and Opera 7+ on Windows 98+, Linux (RH 9, FC 6), and Mac OS X. 4. As usual, the Safari browser is not working correctly,...
15
by: GinnTech | last post by:
I have a site that works perfectly in IE6 IE7 FF2 FF3 but not in the latest Safari. Here is the issue. I am attempting to call functions within a flash object. When trying to attempt to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.