2013-03-27 96 views
1

此作品在全部瀏覽器,但IE瀏覽器。我已檢查的代碼,我知道 IE瀏覽器可以正常讀取,所以你不會不知所措。 爲什麼IE說我的功能不存在?爲什麼這個JS沒有在IE中正確執行?

錯誤:

SCRIPT5009: 'switchStyles' is undefined 
test.php, line 180 character 21 

HTML:

025: <SCRIPT TYPE="text/javascript" SRC="/_js/styleSwitch2.js"><!-- Style Switching Scripts --></SCRIPT> 
... 
180: <SELECT ONCHANGE="return switchStyles(this);"> 
181:  <OPTION VALUE="/_css/default.css">Select a style!</OPTION> 
182:  <OPTGROUP LABEL="Skin Group 1"> 
183:   <OPTION VALUE="/_css/alt1-1.css">Alt 1.1</OPTION> 
184:   <OPTION VALUE="/_css/alt1-2.css">Alt 1.2</OPTION> 
185:   <OPTION VALUE="/_css/alt1-3.css">Alt 1.3</OPTION> 
186:  </OPTGROUP> 
187:  <OPTGROUP LABEL="Skin Group 1"> 
188:   <OPTION VALUE="/_css/alt2-1.css">Alt 2.1</OPTION> 
189:   <OPTION VALUE="/_css/alt2-2.css">Alt 2.2</OPTION> 
190:  </OPTGROUP> 
191: </SELECT> 

JS:

var sheetElement = document.getElementById("_switchSheet"); 
var backupSheetElement = document.getElementById("_switchSheetBackup"); 

sheetElement.disabled = false; 
if (backupSheetElement) 
    backupSheetElement.disabled = false; 
else 
    //warn 

var useCookies = true; 
const COOKIE_NAME = "cookiename"; 


function switchStyles(selectElement) 
{ 
    if (sheetElement == null && (sheetElement = document.getElementById("_switchSheet")) == null) 
     //fail 
    switchStyleTo(selectElement.value); 
} 

function switchStyleTo(newSheet) 
{ 
    sheetElement.href = (newSheet == backupSheetElement.href) ? null : newSheet; 

    setCookie(newSheet); 
} 



function setUseCookies(shouldUseCookies) 
{ 
    useCookies = shouldUseCookies; 
} 

function setCookie(cookieContent) 
{ 
    //works 
} 

function getCookie() 
{ 
    //works 
} 

function setStyleFromCookie() 
{ 
    var cookie = getCookie(); 
    if (cookie.length > 0) 
     switchStyleTo(cookie); 
    else 
     //fail 
} 
+0

其IE瀏覽器您使用? – intelis 2013-03-27 00:27:49

+0

您可能在函數存在之前調用該函數。 – Ryoku 2013-03-27 00:29:08

+0

請作出[jsfiddle](http://jsfiddle.net/)請 – Adidi 2013-03-27 00:30:15

回答

5

,因爲這條線在IE中是錯誤的,阻止其他代碼運行。

const COOKIE_NAME = "cookiename"; 

變更const變成var

或:

try { 
    const COOKIE_NAME = "cookiename"; 
} catch (e) { 
    var COOKIE_NAME = "cookiename"; 
} 
+3

從[MDN](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/const):const的當前實現是Mozilla特定的擴展,不是ECMAScript 5的一部分。它在Firefox和Chrome(V8)中受支持。從Safari 5.1.7和Opera 12.00開始,如果您在這些瀏覽器中使用const定義變量,您仍然可以稍後更改其值。它在Internet Explorer 6-9或Internet Explorer 10的預覽中不受支持。當前,const關鍵字在函數作用域中聲明常量(如使用var聲明的變量)。 – sh0ber 2013-03-27 00:37:49

+0

將其更改爲var works,但try ... catch會導致原始錯誤。謝謝! – Supuhstar 2013-03-27 00:45:56

相關問題