【问题标题】:internet explorer not reading Js fileInternet Explorer 不读取 Js 文件
【发布时间】:2011-11-27 00:39:36
【问题描述】:

我正在使用 JavaScript 覆盖我的网站 3rd 方购物车中的一些预设选项。 该脚本在 Chrome 和 Firefox 中完美运行,但在 Internet Explorer 中根本无法运行。

我的脚本是<script src="http://www.amorame.com/geoff.js"></script>;

alert('test');
function x(){
    var y=document.forms[0].elements["pm"];
    var z;
    for (var j=0; j<y.length; j++) {
        z=y[j];
        if (z.value=="40") {
            z.style.display="none";
        }
    }
    document.body.innerHTML=document.body.innerHTML.replace("Payment by <b>Moneybookers</b> e-wallet<br>","");
    document.body.innerHTML=document.body.innerHTML.replace("Maestro, Visa and other credit/debit cards by <b>Moneybookers</b>","Pago con Diners Club, Mastercard o Visa");
}
onload=x;

我对 JS 很陌生,老实说,我只是想不通解决这个问题的答案,所以 IE 会读取脚本。

【问题讨论】:

    标签: javascript internet-explorer


    【解决方案1】:

    我从来没有听说过 IE 不读取 js 文件,我通常通过添加一个简单的行来验证:

    alert('test');
    

    检查文件是否被加载..

    如果您收到警报,则文件已加载,但代码在 IE 中无法正常工作,这更有可能是 IE 根本没有加载 JS..

    【讨论】:

    • 对不起,伙计们,应该更清楚。 Internet Explorer 正在加载 js,它只是没有执行所有功能,该脚本的目的是替换页面中预先存在的文本,同时从预先设置的表单中删除单选按钮选项。脚本的所有动作在chrome和firefox中都成功执行了,但是explorer中只有remove单选按钮功能起作用,explorer中没有进行文本的replace功能,感谢您的关注!
    【解决方案2】:

    试试:

    &lt;script type="text/javascript" src="http://www.amorame.com/geoff.js"&gt;&lt;/script&gt;;

    并查看脚本是否适用于 alert('test');

    【讨论】:

    • 对不起,伙计们,应该更清楚。 Internet Explorer 正在加载 js,它只是没有执行所有功能,该脚本的目的是替换页面中预先存在的文本,同时从预先设置的表单中删除单选按钮选项。脚本的所有动作在chrome和firefox中都成功执行了,但是explorer中只有remove单选按钮功能起作用,explorer中没有进行文本的replace功能,感谢您的关注!
    • 什么版本的IE?代码可能不兼容。使用跨浏览器的 jQuery。
    【解决方案3】:

    这可能是脚本无法在 IE 下运行,而不是 IE 无法“读取”。你检查过 IE javascript 控制台吗?

    【讨论】:

      【解决方案4】:

      这是 IE 中的一个错误。如果您提醒 innerhtml,您可以看到它已被替换。但是浏览器并没有反映它。

      检查这个 SO 问题:https://stackoverflow.com/questions/1293427/

      你需要使用 DOM 的更多功能来改变它。这是一个生成 dom 的小 sn-p(我的学校作业,所以不要介意虚拟文本):

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
      <html>
        <head>
          <title>Creating the Dom!</title>
          <link rel="stylesheet" type="text/css" href="createDom.css">
          <script type="text/javascript">
      
         function buildbody() 
         {
              var body = document.getElementsByTagName("body")[0];
      
              var theHeader = document.createElement('h1');
              var theHeaderTxt = document.createTextNode('Study program Bachelor of ICT');
              theHeader.appendChild(theHeaderTxt);
              body.appendChild(theHeader);
      
              var theHeader2 = document.createElement('h2');
              var theHeader2Txt = document.createTextNode('Introduction');
              theHeader2.appendChild(theHeader2Txt);
              body.appendChild(theHeader2);
      
              var txtN1 = document.createElement('p');
              var pTxt1 = document.createTextNode(
              'Western society is based on well functioning but rapidly changing technological applications.\n' +
              'However, mere specialisation is no longer enough modern technologistsneed to be capable of forming \n' +
              'a global overview of the developments in their branch. Accordingly, graduates from the Faculty of \n' +
              'Technology of University Drenthe are broadly educated technologists with an eye for innovation, \n' + 
              'management and social circumstances. The Faculty of Technology offers a four-year study program \n' +
              'Bachelor of ICT.');
              txtN1.appendChild(pTxt1);
              body.appendChild(txtN1); 
      
              var theHeader3 = document.createElement('h2');
              var theHeader3Txt = document.createTextNode('Course characters');
              theHeader3.appendChild(theHeader3Txt);
              body.appendChild(theHeader3);
      
              var pTxt2 = document.createTextNode(
              'Work is done on a problem-oriented, project basis, often involving external research, and the \n' +
              'practical training periods are completed at interesting positions in the relevant industries.');
              var txtN2 = document.createElement('p');
              txtN2.appendChild(pTxt2);
              body.appendChild(txtN2); 
      
              var theHeader4 = document.createElement('h2');
              var theHeader4Txt = document.createTextNode('Internship');
              theHeader4.appendChild(theHeader4Txt);
              body.appendChild(theHeader4);
      
              var pTxt3 = document.createTextNode(
              'In the third and fourth year students have the opportunity to put the theory into practice. \n' +
              'A company based traineeship of five months in the third year is part of the program and in the \n' +
              'fourth year students work on a final graduation project for one semester. Both periods can be \n' +
              'spent in the Netherlands or abroad.');
              var txtN3 = document.createElement('p');
              txtN3.appendChild(pTxt3);
              body.appendChild(txtN3); 
      
              var linkN1 = document.createElement('a');
              var linkTxt1 = document.createTextNode('Click for more information');
              linkN1.setAttribute('href','http://www.hbo-i.nl/default.aspx?pageID=24');
              linkN1.appendChild(linkTxt1);
              body.appendChild(linkN1);
         }
          </script>
        </head>
          <body onload="buildbody()">
          </body>
      </html>
      

      如您所见,文档的正文完全是空的,脚本会生成它。您还必须以这种方式更改 dom。

      【讨论】:

        【解决方案5】:

        我在使用 Internet Explorer 时遇到了同样的症状。 这一切都归结为一个箭头函数,IE 不支持。

        【讨论】:

          【解决方案6】:

          IE 似乎完全忽略了其中某处具有非 ES5 语法的文件。我这样做是为了检查语法兼容性:

          npm install -g es-check
          es-check es5 offendingFile.js
          

          【讨论】:

            猜你喜欢
            • 2012-05-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-10-19
            • 2011-02-20
            • 2013-02-24
            • 2015-07-01
            • 2011-02-11
            相关资源
            最近更新 更多