浏览器兼容性(Javascript)

时间:2014-06-24 20:46:50

标签: javascript jquery internet-explorer browser compatibility

我正在使用常规Javascript重写网站的脚本以加快颜色选择器的速度,但是如果你在Firefox和IE中查看它,它就不起作用(但它在Chrome中工作正常)。我真的希望得到一些帮助:

https://www.sinister.ly/index.php

<div id="theme_styler">
    <div class="option default" id="red"></div>
    <div class="option" id="green"></div>
    <div class="option" id="blue"></div> 
</div>

似乎是问题的具体代码:

var optionalStylesheet = document.getElementsByClassName("stylesheet_optional");
var blueStylesheet = document.getElementById("stylesheet_blue");
var greenStylesheet = document.getElementById("stylesheet_green");
var storedThemeColor = readCookie('themeColor');
var currentFiconPath;

function changeColor(path) {
    var all = document.getElementsByClassName("ficon");
    for (var i=0, max=all.length; i < max; i++) {
        all[i].src = all[i].src.replace(/ficons\/((green|blue)\/)?/, 'ficons/' + path + '/');
    }
}

这是标题:

// AD JS

document.getElementsByClassName = function(cl) {
  var retnode = [];
  var elem = this.getElementsByTagName('*');
  for (var i = 0; i < elem.length; i++) {
    if((' ' + elem[i].className + ' ').indexOf(' ' + cl + ' ') > -1) retnode.push(elem[i]);
  }
  return retnode;
}; 

function readCookie(name) {
var ca = document.cookie.split(';');
var nameEQ = name + "=";
for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}

function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}

var optionalStylesheet = document.getElementsByClassName("stylesheet_optional");
var blueStylesheet = document.getElementById("stylesheet_blue");
var greenStylesheet = document.getElementById("stylesheet_green");
var storedThemeColor = readCookie('themeColor');
var currentFiconPath;

function changeColor(path) {
var all = document.getElementsByClassName("ficon");
for (var i=0, max=all.length; i < max; i++) {
        all[i].src = all[i].src.replace(/ficons\/((green|blue)\/)?/, 'ficons/' + path + '/');
    }
}

var gs = document.createElement("link");
gs.type = "text/css";
gs.className = "stylesheet_optional";
gs.id = "stylesheet_blue";
gs.rel = "stylesheet";
gs.title = "mystyle";
gs.href = "images/sinisterly/color_blue.css";

var bs = document.createElement("link");
bs.type = "text/css";
bs.className = "stylesheet_optional";
bs.id = "stylesheet_green";
bs.rel = "stylesheet";
bs.title = "mystyle";
bs.href = "images/sinisterly/color_green.css";

var head = document.getElementsByTagName("head")[0];
var links = head.getElementsByTagName("link");
for(var x=0; x<links.length; x++) {
   var href = links[x].href;
    if(href.indexOf('/color_green.css') >0 || href.indexOf('/color_blue.css') >0){
      head.removeChild(links[x]);
   }
}

if (storedThemeColor == "green"){
    head.appendChild(bs);
} else if (storedThemeColor == "blue") {
    head.appendChild(gs);
} 

// End AD JS

这是在页脚中

if (storedThemeColor == null) {
    setCookie('themeColor', 'default', 7);
    currentFiconPath = '';
    return currentFiconPath;
} else if (storedThemeColor != null) {
    if (storedThemeColor == 'default') {
        currentFiconPath = '';
        optionalStylesheet.disabled = true;
        return currentFiconPath;
    } else if (storedThemeColor == 'blue') {
        currentFiconPath = "blue";
        changeColor(currentFiconPath);
        optionalStylesheet.disabled = true;
        blueStylesheet.disabled = false;
        return currentFiconPath;
    } else if (storedThemeColor == 'green') {
        currentFiconPath = "green";
        changeColor(currentFiconPath);
        optionalStylesheet.disabled = true;
        greenStylesheet.disabled = false;
        return currentFiconPath;
    }
}

1 个答案:

答案 0 :(得分:2)

变化:

var blueStylesheet = document.getElementById("#stylesheet_blue");
var greenStylesheet = document.getElementById("#stylesheet_green");

为:

var blueStylesheet = document.getElementById("stylesheet_blue");
var greenStylesheet = document.getElementById("stylesheet_green");

样式表ID的开头没有#

我不知道为什么它在Chrome中运行 - 当我尝试时,我在Chrome中获得undefined