2011-02-27 38 views
0

我在javascript中創建了一個巨大的數組......它有4個級別深度和許多元素長。我在運行時使用w/php創建它,這樣頁面上的所有內容都可以順利運行,無需查詢。JavaScript數組短路腳本

但是,現在,我有一些問題的數組和我的HTML文件沒有看到數組後列出的JavaScript函數。通過測試,我將這個數組縮短了,並使其運行起來,但對於我來說,我無法弄清楚爲什麼它會以完整形式切斷腳本的其餘部分。

我將不勝感激任何答案,要麼告訴我 A.如何使用Firebug發現問題(我很新的螢火蟲) 或 B.告訴我#$%^是什麼問題!

我將在下面列出我的整個腳本(來自php的輸出)。有問題的陣列是「inventoryNames」。我已經檢查過這個問題是否是棘突的數量,但我現在已經99%了。如果我評論它一切正常。由於

<script type="text/javascript"> 

var inventoryNames = 
     Array(
      Array(
       Array(
        Array("AC/DC", "Back in Black", "36", "VG++", "12", ""), 
        Array("AC/DC", "Back in Black", "37", "VG", "8", "") 
       ), 
       Array(
        Array("AC/DC", "Highway to Hell", "30", "VG++", "10", ""), 
        Array("AC/DC", "Highway to Hell", "38", "VG", "5", ""), 
        Array("AC/DC", "Highway to Hell", "35", "NM", "16", ""), 
        Array("AC/DC", "Highway to Hell", "39", "NM", "14", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Al Green", "Call Me", "40", "G", "5", ""), 
        Array("Al Green", "Call Me", "31", "NM", "15", "") 
       ), 
       Array(
        Array("Al Green", "Let's Stay Together", "32", "NM", "12", ""), 
        Array("Al Green", "Let's Stay Together", "33", "VG++", "10", ""), 
        Array("Al Green", "Let's Stay Together", "34", "VG", "8", "") 
       ) 
      ), 
      Array(
       Array(
        Array("B.B. King", "Confessin' the Blues", "45", "VG++", "9", ""), 
        Array("B.B. King", "Confessin' the Blues", "42", "NM", "35", "") 
       ), 
       Array(
        Array("B.B. King", "Lucille", "43", "VG++", "14", "") 
       ), 
       Array(
        Array("B.B. King", "Singin' the Blues", "41", "NM", "16", "") 
       ), 
       Array(
        Array("B.B. King", "Sings Spirituals", "44", "VG++", "10", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Bad Company", "Bad Company", "12", "G", "2", ""), 
        Array("Bad Company", "Bad Company", "13", "M", "38", "1st Edition"), 
        Array("Bad Company", "Bad Company", "14", "VG+", "8", ""), 
        Array("Bad Company", "Bad Company", "3", "VG++", "13", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Bob Dylan", "Another Side of Bob Dylan", "48", "VG++", "20", "") 
       ), 
       Array(
        Array("Bob Dylan", "Blonde on Blonde", "50", "M", "35", "") 
       ), 
       Array(
        Array("Bob Dylan", "Highway 61 Revisited", "49", "NM", "25", "") 
       ), 
       Array(
        Array("Bob Dylan", "The Freewheelin' Bob Dylan", "46", "VG++", "13", "") 
       ), 
       Array(
        Array("Bob Dylan", "The Times They Are A-Changin'", "47", "NM", "25", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Boston", "Boston", "52", "VG++", "12", ""), 
        Array("Boston", "Boston", "53", "VG++", "10", ""), 
        Array("Boston", "Boston", "51", "VG", "7", ""), 
        Array("Boston", "Boston", "23", "VG", "6", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Charlie Parker", "Jazz at Massey Hall", "54", "NM", "42", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Creedence Clearwater Revival", "Green River", "56", "VG++", "13", ""), 
        Array("Creedence Clearwater Revival", "Green River", "57", "NM", "16", ""), 
        Array("Creedence Clearwater Revival", "Green River", "55", "VG+", "10", "") 
       ), 
       Array(
        Array("Creedence Clearwater Revival", "Willy and the Poor Boys", "60", "NM", "15", ""), 
        Array("Creedence Clearwater Revival", "Willy and the Poor Boys", "114", "NM", "16", ""), 
        Array("Creedence Clearwater Revival", "Willy and the Poor Boys", "58", "G", "5", ""), 
        Array("Creedence Clearwater Revival", "Willy and the Poor Boys", "59", "VG", "7", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Dave Brubeck", "Time Out", "61", "NM", "18", ""), 
        Array("Dave Brubeck", "Time Out", "62", "VG+", "10", "") 
       ) 
      ), 
      Array(
       Array(
        Array("David Allan Coe", "Longhaired Redneck", "63", "VG+", "8", "") 
       ), 
       Array(
        Array("David Allan Coe", "Rides Again", "64", "VG+", "6", ""), 
        Array("David Allan Coe", "Rides Again", "65", "NM", "10", "") 
       ) 
      ), 
      Array(
       Array(
        Array("David Bowie", "Hunky Dory", "18", "NM", "40", "Rare!") 
       ), 
       Array(
        Array("David Bowie", "Ziggy Stardust and the Spiders From Mars", "21", "VG++", "15", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Earth, Wind & Fire", "Earth, Wind & Fire", "67", "VG+", "10", "") 
       ), 
       Array(
        Array("Earth, Wind & Fire", "That's the Way of the World", "66", "NM", "15", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Foghat", "Energized", "68", "NM", "10", ""), 
        Array("Foghat", "Energized", "2", "M", "2", "") 
       ) 
      ), 
      Array(
       Array(
        Array("George Harrison", "All Things Must Pass", "69", "NM", "35", ""), 
        Array("George Harrison", "All Things Must Pass", "1", "M", "2", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Guns N Roses", "Appetite for Destruction", "70", "NM", "20", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Hank Williams", "Hank Williams Sings", "29", "M", "55", ""), 
        Array("Hank Williams", "Hank Williams Sings", "71", "NM", "70", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Herbie Hancock", "Empyrean Isles", "72", "VG++", "10", "") 
       ), 
       Array(
        Array("Herbie Hancock", "Head Hunters", "74", "VG++", "13", "") 
       ), 
       Array(
        Array("Herbie Hancock", "Maiden Voyahe", "73", "VG++", "13", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Huey Lewis & the News", "Sports", "76", "VG+", "3", ""), 
        Array("Huey Lewis & the News", "Sports", "77", "VG", "2", ""), 
        Array("Huey Lewis & the News", "Sports", "75", "NM", "8", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Its a Beautiful Day", "Its a Beautiful Day", "20", "VG++", "10", "") 
       ) 
      ), 
      Array(
       Array(
        Array("James Brown", "Prisoner of Love", "78", "VG++", "18", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Jimi Hendrix", "Are You Experienced", "22", "VG+", "12", "") 
       ) 
      ), 
      Array(
       Array(
        Array("John Lee Hooker", "John Lee Hooker Plays and Sings the Blues", "81", "VG++", "15", "") 
       ), 
       Array(
        Array("John Lee Hooker", "Travelin'", "80", "VG++", "15", ""), 
        Array("John Lee Hooker", "Travelin'", "82", "VG+", "15", ""), 
        Array("John Lee Hooker", "Travelin'", "79", "VG++", "20", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Mazaak", "Fat Man Suicide", "25", "M", "200", "Includes classics like "Cream Green and Beans" and the title track "Fat Man Suicide"!") 
       ) 
      ), 
      Array(
       Array(
        Array("Miles Davis", "Bitches Brew", "85", "NM", "30", "") 
       ), 
       Array(
        Array("Miles Davis", "Kind of Blue", "84", "VG+", "15", ""), 
        Array("Miles Davis", "Kind of Blue", "83", "VG", "10", "") 
       ), 
       Array(
        Array("Miles Davis", "Miles Ahead", "87", "NM", "15", "") 
       ), 
       Array(
        Array("Miles Davis", "Round About Midnight", "86", "NM", "45", "") 
       ), 
       Array(
        Array("Miles Davis", "Star People", "88", "M", "15", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Motley Crue", "Theatre of Pain", "4", "VG", "5", ""), 
        Array("Motley Crue", "Theatre of Pain", "16", "VG", "5", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Muddy Waters", "At Newport", "89", "VG+", "10", "") 
       ), 
       Array(
        Array("Muddy Waters", "Hard Again", "90", "VG++", "15", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Otis Redding", "In Person at the Whisky a Go Go", "28", "VG++", "20", "") 
       ), 
       Array(
        Array("Otis Redding", "Otis Blue", "92", "VG+", "12", "") 
       ), 
       Array(
        Array("Otis Redding", "The Dock of the Bay", "91", "VG++", "20", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Ozzy Osbourne", "Bark at the Moon", "95", "VG++", "15", "") 
       ), 
       Array(
        Array("Ozzy Osbourne", "Blizzard of Ozz", "93", "VG++", "10", ""), 
        Array("Ozzy Osbourne", "Blizzard of Ozz", "94", "VG++", "12", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Parliament", "Mothership Connection", "96", "VG", "8", "") 
       ), 
       Array(
        Array("Parliament", "Motor Booty Affair", "97", "NM", "12", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Pink Floyd", "Wish You Were Here", "15", "NM", "17", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Sly & the Family Stone", "Stand!", "100", "NM", "20", ""), 
        Array("Sly & the Family Stone", "Stand!", "98", "VG", "8", ""), 
        Array("Sly & the Family Stone", "Stand!", "99", "VG++", "15", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Taj Mahal", "Giant Step", "6", "VG+", "13", ""), 
        Array("Taj Mahal", "Giant Step", "19", "VG+", "10", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Talking Heads", "Remain in Light", "101", "VG++", "15", ""), 
        Array("Talking Heads", "Remain in Light", "102", "VG+", "10", ""), 
        Array("Talking Heads", "Remain in Light", "103", "NM", "20", "") 
       ), 
       Array(
        Array("Talking Heads", "Talking Heads 77", "104", "NM", "25", "") 
       ) 
      ), 
      Array(
       Array(
        Array("The Allman Brothers Band", "At Fillmore East", "105", "VG++", "15", ""), 
        Array("The Allman Brothers Band", "At Fillmore East", "106", "VG++", "15", ""), 
        Array("The Allman Brothers Band", "At Fillmore East", "11", "NM", "16", "") 
       ), 
       Array(
        Array("The Allman Brothers Band", "Eat a Peach", "8", "G", "3", ""), 
        Array("The Allman Brothers Band", "Eat a Peach", "9", "VG", "8", ""), 
        Array("The Allman Brothers Band", "Eat a Peach", "10", "M", "20", "") 
       ) 
      ), 
      Array(
       Array(
        Array("The Beatles", "Rubber Soul", "24", "M", "1.93", "") 
       ), 
       Array(
        Array("The Beatles", "Sgt. Pepper's Lonely Hearts Club Band", "5", "VG+", "15", ""), 
        Array("The Beatles", "Sgt. Pepper's Lonely Hearts Club Band", "27", "VG++", "20", "") 
       ) 
      ), 
      Array(
       Array(
        Array("The Mothers of Invention", "Freak Out!", "17", "NM", "20", "") 
       ) 
      ), 
      Array(
       Array(
        Array("The Rolling Stones", "Exile on Main Street", "107", "NM", "25", "") 
       ), 
       Array(
        Array("The Rolling Stones", "Let It Bleed", "7", "M", "12", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Thelonious Monk", "Brilliant Corners", "108", "VG++", "30", ""), 
        Array("Thelonious Monk", "Brilliant Corners", "109", "M", "60", "") 
       ), 
       Array(
        Array("Thelonious Monk", "Monk's Dream", "110", "VG++", "19", ""), 
        Array("Thelonious Monk", "Monk's Dream", "26", "NM", "35", "") 
       ) 
      ), 
      Array(
       Array(
        Array("Willie Nelson", "Phases and Stages", "113", "VG++", "12", "") 
       ), 
       Array(
        Array("Willie Nelson", "Red Headed Stranger", "112", "VG++", "11", ""), 
        Array("Willie Nelson", "Red Headed Stranger", "111", "VG++", "10", "") 
       ) 
      ) 
     ); 











var albumNames = Array(Array("Highway to Hell", "Back in Black"), Array("Call Me", "Let's Stay Together"), Array("Singin' the Blues", "Confessin' the Blues", "Sings Spirituals", "Lucille", "Live at the Regal"), Array("Bad Company"), Array("The Freewheelin' Bob Dylan", "The Times They Are A-Changin'", "Another Side of Bob Dylan", "Bringing It All Back Home", "Highway 61 Revisited", "Blonde on Blonde", "John Wesley Harding", "Nashville Skyline", "Self Portrait", "New Morning", "Blood on the Tracks"), Array("Boston"), Array("Jazz at Massey Hall"), Array("Green River", "Cosmos Factory", "Willy and the Poor Boys"), Array("Time Out", "The Dave Brubeck Quartet at Carnegie Hall"), Array("Longhaired Redneck", "Rides Again"), Array("Ziggy Stardust and the Spiders From Mars", "Hunky Dory"), Array("That's the Way of the World", "Earth, Wind & Fire"), Array("Energized"), Array("All Things Must Pass"), Array("Appetite for Destruction"), Array("Hank Williams Sings"), Array("Empyrean Isles", "Maiden Voyahe", "Head Hunters", "Sextant", "Crossings"), Array("Sports", "Picture This"), Array("Its a Beautiful Day"), Array("Prisoner of Love"), Array("Are You Experienced"), Array("Travelin'", "John Lee Hooker Plays and Sings the Blues"), Array("Fat Man Suicide"), Array("Round About Midnight", "Miles Ahead", "Kind of Blue", "Bitches Brew", "Star People"), Array("Theatre of Pain"), Array("At Newport", "Folk Singer", "Brass and the Blues", "Hard Again"), Array("The Dock of the Bay", "Otis Blue", "In Person at the Whisky a Go Go"), Array("Blizzard of Ozz", "Bark at the Moon"), Array("Mothership Connection", "Motor Booty Affair"), Array("Wish You Were Here"), Array("Look What the Cat Dragged In"), Array("The Genius of Ray Charles", "Modern Sounds in Country and Western Music", "The Genius Sings the Blues"), Array("Stand!", "Fresh"), Array("Giant Step", "Taj Mahal", "Happy Just to Be Like I Am", "Music Keeps Me Together"), Array("Remain in Light", "Talking Heads 77", "Speaking in Tongues"), Array("Eat a Peach", "At Fillmore East"), Array("Please Please Me", "A Hard Day's Night", "Help!", "Rubber Soul", "Revolver", "Sgt. Pepper's Lonely Hearts Club Band", "Magical Mystery Tour", "The Beatles (White Album)", "Abbey Road", "Let It Be"), Array("Freak Out!"), Array("Exile on Main Street", "Big Hits (High Tide and Green Grass)", "Let It Bleed"), Array("Brilliant Corners", "Evidence", "Monk's Dream"), Array("Red Headed Stranger", "Yesterday's Wine", "Phases and Stages", "The Sound in Your Mind")); 

function byId(id){ 
     return document.getElementById ? document.getElementById(id) : document.all[id]; 
    } 
    function populateAlbums(index) { 
     var i = byId('albumSelect').options.length; 
     for (; i>=0; i--) { 
      byId('albumSelect').remove(i); 
     } 

     var artistAlbums = albumNames[index]; 
     for (i = 0; i < artistAlbums.length; i++) { 
     var option = document.createElement('option'); 
     option.text = artistAlbums[i]; 
     option.value = artistAlbums[i]; 
     try  { byId('albumSelect').add(option, null); } // non-IE 
     catch(e) { byId('albumSelect').add(option); } // IE 
     } 
    } 
</script> 
+1

鬆散數組並僅使用方括號'var arr = [[1,2,3],[4,5,6],[7,8,9]];' – epascarello 2011-02-27 19:42:46

+0

我不知道您的場景但是如果你打算在每個頁面請求發送這個消息,你可能會浪費很多帶寬。 – pimvdb 2011-02-27 19:42:59

+0

您可能需要考慮使用JavaScript數組常量符號:'[「a」,「b」,「c」]'而不是「Array()」構造函數。 – Pointy 2011-02-27 19:43:02

回答

5

你的一個字符串中包含雙引號:

Array("Mazaak", "Fat Man Suicide", "25", "M", "200", "Includes classics like "Cream Green and Beans" and the title track "Fat Man Suicide"!") 

你可以用黑色斜線

Array("Mazaak", "Fat Man Suicide", "25", "M", "200", "Includes classics like \"Cream Green and Beans\" and the title track \"Fat Man Suicide\"!") 

或使用字符串周圍的單引號轉義

Array("Mazaak", "Fat Man Suicide", "25", "M", "200", 'Includes classics like "Cream Green and Beans" and the title track "Fat Man Suicide"!') 
1

Array("Mazaak", "Fat Man Suicide", "25", "M", "200", "Includes classics like "Cream Green and Beans" and the title track "Fat Man Suicide"!")

您的字符串中沒有您的文字",因此代碼被破壞。進入php並做一些替換"\"

1

你不是逃避你打印在你的JS代碼中的字符串。

當JS中存在語法錯誤時,腳本的其餘部分甚至沒有被評估,所以在語法錯誤之下你永遠找不到任何東西。

三點建議:

  • 使用addslashes打印字符串之前。
  • 使用緊湊語法做磁盤陣列:

例子:

var inventoryNames = 
    [ 
     [ 
      [ 
       ["AC/DC", "Back in Black", "36", "VG++", "12", ""], 
       ["AC/DC", "Back in Black", "37", "VG", "8", ""] 
      ), 
      [ 
       ["AC/DC", "Highway to Hell", "30", "VG++", "10", ""], 
       ["AC/DC", "Highway to Hell", "38", "VG", "5", ""], 
       ["AC/DC", "Highway to Hell", "35", "NM", "16", ""], 
       ["AC/DC", "Highway to Hell", "39", "NM", "14", ""] 
      ] 
     ], 
  • 安置自己的JS/HTML使用類似jsfiddle.net相關的問題。

祝你好運!