2012-04-25 3 views
1

이 기능은 일부 IE 브라우저에서 작동하지만 전혀 작동하지 않습니다. IE 8은 나에게 오류를 제공합니다JavaScript .split 기능이 일부 브라우저에서 작동하지 않습니다.

웹 페이지 오류 세부

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.3; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET4.0C; .NET4.0E; MS-RTC LM 8) Timestamp: Wed, 25 Apr 2012 15:18:21 UTC

Message: Object doesn't support this property or method 
Line: 9 
Char: 17 
Code: 0 
URI: file:///G:/1.html 

코드 :

GetLink(); 

function GetLink() { 
    selectedOption = "asdasdasd: asdasdas|asdadasd:asdadsasd|asdasdasd:asdasdad"; 
    ROOM = selectedOption.split("|")[0].trim().split(":")[1].trim(); 
    BUILDING = selectedOption.split("|")[1].trim().split(":")[1].trim(); 
    var ret = "room_chart.jsp?room=" + ROOM + "&building=" + BUILDING; 
    return ret; 
} 

답변

6

split 방법은 괜찮습니다, 그것은 문제를 일으키는 trim입니다. 당신이 사용할 수있는이 polyfill from MDN 작은 :

if(!String.prototype.trim) { 
    String.prototype.trim = function() { 
    return this.replace(/^\s+|\s+$/g,''); 
    }; 
} 

String.prototype.trim는 이미 존재하지 않는 경우 단순히 String.prototypesplit 방법을 추가 위의 IE < 9 코드의 조각을 사용할 수 없습니다, 당신이 예상하는대로 정확하게 동작 에 대한 네이티브 구현.

jQuery를 사용하는 경우 사용할 수있는 $.trim 방법이 있습니다.

+0

또한 잠재적으로 더 빠른 이러한 손질 방법 중 일부를 시도해 볼 수 있습니다. http://blog.stevenlevithan.com/archives/faster-trim-javascript – Azmisov

관련 문제