View Full Version : Safari Javascript Problem
slughead
Oct 22, 2006, 09:57 AM
Apparently, Safari translates this array ...:
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:
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
Oct 22, 2006, 10:29 AM
Apparently, Safari translates this array ...:
...
into this string:
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
Oct 22, 2006, 11:25 AM
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:
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
Oct 22, 2006, 01:37 PM
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):
<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:
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
Oct 22, 2006, 02:01 PM
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
Oct 22, 2006, 02:10 PM
Nevermind. I did some serious googling and found this:
function isArray(obj) { // this works
return obj.constructor == Array;
}
THAT WORKS, this does not:
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!
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.