Safari Javascript Problem

Discussion in 'Mac Programming' started by slughead, Oct 22, 2006.

  1. slughead macrumors 68030

    slughead

    Joined:
    Apr 28, 2004
    #1
    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?
     
  2. MrFrankly macrumors regular

    Joined:
    Jan 11, 2006
    #2
    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.
     
  3. slughead thread starter macrumors 68030

    slughead

    Joined:
    Apr 28, 2004
    #3

    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
     
  4. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #4
    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.
     
  5. slughead thread starter macrumors 68030

    slughead

    Joined:
    Apr 28, 2004
    #5
    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?
     
  6. slughead thread starter macrumors 68030

    slughead

    Joined:
    Apr 28, 2004
    #6
    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!
     

Share This Page