0

HEllo guys i have a script that run fine in Chrome and FF but seems not working in IE... Although it has a java script error and i cannot solve it the error is

Uncaught TypeError: Object function (){var b=this;var d=[];for(var a=b.length;a--;){var c=b[a];if(jQuery.inArray(c,d)===-1)d.unshift(c)}return d} has no method 'split'

Please help

    function populate(s1,s2){
    var s1 = document.getElementById(s1);
    var s2 = document.getElementById(s2);
    s2.innerHTML = "";
    if(s1.value == "bedroom"){
        var optionArray = ["|Room Type","private|Private","shared|Shared","suite|Suite"];
    } else if(s1.value == "house"){
        var optionArray = ["|House Type","beach_house|Beach House","bungalow|Bungalow","cabin|Cabin","chateau|Chateau","house|House","cottage|Cottage","lake_house|Lake House","ski_chalet|Ski Chalet","townhosue|Townhouse","villa|Villa"];
    } else if(s1.value == "apartment"){
        var optionArray = ["|Appartment Type","apartment|Apartment","apartment_hotel|Apartment Hotel","condo|Condo","loft|Loft","luxury_apartment|Luxury Apartment","serviced_aparment|Serviced Apartment"];
    } else if(s1.value == "bnb"){
        var optionArray = ["|B&B Type","room|Room","suite|Suite"];
    } else if(s1.value == "hotel"){
        var optionArray = ["|Hotel Type","boutique_hotel|Boutique Hotel","budget_hotel|Budget Hotel","hotel|Hotel","inn|Inn","resort|Resort"];
    } else if(s1.value == "hostel"){
        var optionArray = ["|Hostel Type","hostel|Hostel"];
    }
    for(var option in optionArray){
        var pair = optionArray[option].split("|");
        var newOption = document.createElement("option");
        newOption.value = pair[0];
        newOption.innerHTML = pair[1];
        s2.options.add(newOption);
    }
}
  • add a console.log before split, one of your `optionArray[option]` is probably undefined – slash197 Sep 10 '13 at 10:25
  • Before your `for` loop, check if `optionArray` is defined. – hsz Sep 10 '13 at 10:25
  • 1) Avoid `for-in` on arrays, at least without special reason; 2) looks like the assumed value `optionArray[option]` is not being found. Try console logging it before running `split` on it. – Mitya Sep 10 '13 at 10:25
  • you are defining var optionArray in if and some else if, but not in a generic else, so option array can be undefined. What is the value of s1.value when you face the issue? – Irvin Dominin Sep 10 '13 at 10:27

2 Answers2

0

It looks like you are using some extension that adds properties to the array prototype, so IE operates on additional values that other browsers do not. The direct fix is to not use for..in on your array, use a plain for instead:

for(var i = 0; i < optionArray.length; ++i) {
    // and now use optionArray[i] instead of optionArray[option]
}

Another option would be to filter out these properties with

for(var option in optionArray){
    if (!optionArray.hasOwnProperty(option)) continue;
    // the rest without modification
}
Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
0

You are defining the var optionArray inside an if and some else if, but not in a generic else, so in some case optionArray can be undefined.

You can move the definition at the top of the if, or define a generic else.

Code:

function populate(s1,s2){
    var s1 = document.getElementById(s1);
    var s2 = document.getElementById(s2);
    s2.innerHTML = "";
    var optionArray=new Array();
    if(s1.value == "bedroom"){
        optionArray = ["|Room Type","private|Private","shared|Shared","suite|Suite"];
    } else if(s1.value == "house"){
        optionArray = ["|House Type","beach_house|Beach House","bungalow|Bungalow","cabin|Cabin","chateau|Chateau","house|House","cottage|Cottage","lake_house|Lake House","ski_chalet|Ski Chalet","townhosue|Townhouse","villa|Villa"];
    } else if(s1.value == "apartment"){
        optionArray = ["|Appartment Type","apartment|Apartment","apartment_hotel|Apartment Hotel","condo|Condo","loft|Loft","luxury_apartment|Luxury Apartment","serviced_aparment|Serviced Apartment"];
    } else if(s1.value == "bnb"){
        optionArray = ["|B&B Type","room|Room","suite|Suite"];
    } else if(s1.value == "hotel"){
        optionArray = ["|Hotel Type","boutique_hotel|Boutique Hotel","budget_hotel|Budget Hotel","hotel|Hotel","inn|Inn","resort|Resort"];
    } else if(s1.value == "hostel"){
        optionArray = ["|Hostel Type","hostel|Hostel"];
    }
    for(var option in optionArray){
        var pair = optionArray[option].split("|");
        var newOption = document.createElement("option");
        newOption.value = pair[0];
        newOption.innerHTML = pair[1];
        s2.options.add(newOption);
    }
}
Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111