Mac Safari Javascript Problem

slughead

macrumors 68040
Original poster
Apr 28, 2004
3,104
236
Apparently, Safari translates this array ...:
Code:
var happy_cakes = new Array(
new Array(0,'Edit','posting.php?mode=editpost&sid=09cf5dfc5ec6e04fde4982a19e31b084&t=33'),
new Array(0,'Print','print.php?sid=09cf5dfc5ec6e04fde4982a19e31b084&t=33'),
new Array(3,'Set As Profile Text','padmin/set_as_profile.php?sid=09cf5dfc5ec6e04fde4982a19e31b084&t=33&user_id=2'),
new Array(2,'Add Article to Blog ...','mode=blog_list','sub_mode=add'),
new Array(2,'Remove Article From Blog ...','mode=blog_list','sub_mode=remove'));
into this string:
Code:
0,Edit,posting.php?mode=editpost&sid=b0492efc29a57f301dec65f3a2b82a87&t=33,0,Print,print.php?sid=b0492efc29a57f301dec65f3a2b82a87&t=33,3,Set As Profile Text,padmin/set_as_profile.php?sid=b0492efc29a57f301dec65f3a2b82a87&t=33&user_id=2,2,Add Article to Blog ...,mode=blog_list,sub_mode=add,2,Remove Article From Blog ...,mode=blog_list,sub_mode=remove
Can someone help me out?
 

MrFrankly

macrumors regular
Jan 11, 2006
110
0
slughead said:
Apparently, Safari translates this array ...:
...
into this string:
Code:
0,Edit,posting.php?mode=editpost&sid=b0492efc29a57f301dec65f3a2b82a87&t=33,0,Print,print.php?sid=b0492efc29a57f301dec65f3a2b82a87&t=33,3,Set As Profile Text,padmin/set_as_profile.php?sid=b0492efc29a57f301dec65f3a2b82a87&t=33&user_id=2,2,Add Article to Blog ...,mode=blog_list,sub_mode=add,2,Remove Article From Blog ...,mode=blog_list,sub_mode=remove
Can someone help me out?
Works fine for me.

alert(happy_cakes[0][1]) alerts 'Edit'

alert(happy_cakes) indeed alerts a string of the entire array. But that's how it supposed to be.
 

slughead

macrumors 68040
Original poster
Apr 28, 2004
3,104
236
MrFrankly said:
Works fine for me.

alert(happy_cakes[0][1]) alerts 'Edit'

alert(happy_cakes) indeed alerts a string of the entire array. But that's how it supposed to be.

sorry I forgot to mention

put that in a .js file and load it using this script:
Code:
function dhtmlLoadScript(url)
{
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e);
}
Note: the exact same code works in firefox
 

Sayer

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
I tried variations of your code in Safari and Firefox 1.5, but it wouldn't work at first.

If I try to get the array during loading it fails. If I access the array after loading has completely finished, it works in Safari & Firefox.

This works fine in Safari 2.0.4 (slightly different external script file loader):

Code:
<html>
<head>
<title>test</title>
<script language="javascript">
function dhtmlLoadScript(url)
{
  var head = document.getElementsByTagName('head').item(0)
  var scriptTag = document.getElementById('loadScript');
  if(scriptTag) head.removeChild(scriptTag);
  script = document.createElement('script');
  script.src = url;
	script.type = 'text/javascript';
	script.id = 'loadScript';
	head.appendChild(script)

}

</script>
</head>

<body onLoad="javascript:dhtmlLoadScript('myArray.js');">


<p>test</p>

<a href="javascript:alert(myArray[0][1]);">Click test</a>
</body>

</html>
myArray.js contains:

Code:
var myArray = new Array(new Array(0,'Edit','posting.php?mode=editpost&sid=09cf5dfc5ec6e04fde4982a19e31b084&t=33'),
new Array(0,'Print','print.php?sid=09cf5dfc5ec6e04fde4982a19e31b084&t=33'),
new Array(3,'Set As Profile Text','padmin/set_as_profile.php?sid=09cf5dfc5ec6e04fde4982a19e31b084&t=33&user_id=2'),
new Array(2,'Add Article to Blog ...','mode=blog_list','sub_mode=add'),
new Array(2,'Remove Article From Blog ...','mode=blog_list','sub_mode=remove'));
Not to nit-pick, but it is generally considered bad form to use single-letter variable names. You may know what they all mean, but if someone else has to go over your code later on, it is very difficult to follow with single-letter variable names. Always use clear, concise variable and function names in your code.
 

slughead

macrumors 68040
Original poster
Apr 28, 2004
3,104
236
Most of my variable names are really long unless they are going to stop being used within 6 or fewer lines.

OK I found what the problem is: I was using a function to check whether it wasn an array or not, and it wouldn't work in Safari.

In fact, I tried 3 different methods of trying to tell if it's an array or not and none worked!

So my next question is: How can you tell if something is an array in safari?
 

slughead

macrumors 68040
Original poster
Apr 28, 2004
3,104
236
Nevermind. I did some serious googling and found this:

Code:
function isArray(obj) { // this works 
    return obj.constructor == Array;
}
THAT WORKS, this does not:

Code:
function isArray(obj) {
    if (obj.constructor.toString().indexOf("Array") == -1) {
        return false;
    } else {
        return true;
    }
}
There are 2 other methods which also don't work but I don't want to go dig them up again.

A word of warning! test EVERYTHING!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.