HTML头外的JavaScript和样式不适用于生产服务器

时间:2014-01-20 07:35:17

标签: wordpress apache localhost production-environment

我有一个与内联javascript和css相关的奇怪问题。在我的localhost(WAMP服务器2.2)上,一切正常(内联样式和脚本使用并正常工作),但在生产服务器(共享hostgator服务器)上,内联脚本完全被浏览器忽略。如果我简化并给你一个我的意思的例子:

<html>
   <head>
      <!--normal head includes, jquery, styles and other stuff -->
   </head>
   <body>
   <script type="text/javascript">
      jQuery(document).ready(function() {
         alert("test");
      });
   </script>
   <style type="text/css">
      a { color:red; }
   </style>
   <a href="http://www.google.com">Some link</a>
   </body>
</html>

在localhost上,“某些链接”将显示为红色,并会弹出警告。但是在生产服务器上没有发生这种情况。

服务器上的哪种设置(我认为这与安全相关)使这成为可能,我该如何更改它以便使用内联脚本和样式?

1 个答案:

答案 0 :(得分:0)

以编程方式执行此操作:

$(document).ready(function(){
    var head = document.getElementsByTagName('head')[0],
    style = document.createElement('style'),
    css = 'a { color: red }';

    style.type = 'text/css';

    if (style.styleSheet) {
        style.styleSheet.cssText = css;
    }
    else {
        style.appendChild(document.createTextNode(css));
    }
    head.appendChild(style);
});

我希望它有所帮助。 PS:好的做法:在正文结束标记之前加载脚本。只在头标记中加载css。