这适用于所有浏览器,但IE。我已经审查了我知道 IE可以正确读取的代码,所以你不会不知所措。 为什么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 :(得分:5)
因为这行是IE中的错误,导致其余代码无法运行。
const COOKIE_NAME = "cookiename";
将const
更改为var
。
或:
try {
const COOKIE_NAME = "cookiename";
} catch (e) {
var COOKIE_NAME = "cookiename";
}