为什么我的网站上的脚本出现此错误?

时间:2012-08-14 04:24:13

标签: javascript jquery css menubar

我正在尝试通过添加以下一系列代码来解决我在我的网站中使用JQuery LavaLamp菜单栏所面临的问题:

<script>
if ($.browser.version < 9.0 && $.browser.msie) {
document.getElementsByTagName("head")[0].innerHTML = '<script type="text/javascript" src="./Scripts/jquery.easing.1.1.js"></script><script type="text/javascript" src="./Scripts/jquery.preloader.js"></script><script type="text/javascript"  src="./Scripts/jquery.lavalamp.js"></script><script type="text/javascript" src="./Scripts/lavalamp-config.js"></script>';
}
else {
    document.getElementById("head")[0].innerHTML = '<script type="text/javascript" src="./Scripts/jquery.easing.1.1.js"></script><script type="text/javascript" src="./Scripts/jquery.preloader.js">';
}

​​</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​

在Visual Studio 2010中,我收到以下错误

  

未终止的字符串常量

在以下行中:

document.getElementsByTagName("head")[0].innerHTML = '<script type="text/javascript" src="./Scripts/jquery.easing.1.1.js"></script><script type="text/javascript" src="./Scripts/jquery.preloader.js"></script><script type="text/javascript"  src="./Scripts/jquery.lavalamp.js"></script><script type="text/javascript" src="./Scripts/lavalamp-config.js"></script>';

那我该如何解决这个问题呢?

更新#1:

我将我的代码更新为你建议的人,我仍然得到同样的错误。此外,这是错误的快照:

enter image description here

更新#2 我修改了我的代码,让母版页包含以下代码,我仍然得到相同的错误,显示在页面顶部,如上面的快照所示。

<head>
<script type="text/javascript">
function getInternetExplorerVersion()
    // Returns the version of Internet Explorer or a -1
    // (indicating the use of another browser).
    {
        var rv = -1; // Return value assumes failure.
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null)
                rv = parseFloat(RegExp.$1);
        }
        return rv;
    }
</script>

<script type="text/javascript">
 if (getInternetExplorerVersion() < 9.0 && browser_type  = "Microsoft Internet Explorer") {
    document.getElementsByTagName("head")[0].innerHTML = "<script  type='text/javascript' src='./Scripts/jquery.easing.1.1.js'></script><script type='text/javascript' src='./Scripts/jquery.preloader.js'></script><script type='text/javascript'  src='./Scripts/jquery.lavalamp.js'></script><script type='text/javascript' src='./Scripts/lavalamp-config.js'></script>";
 }  else {
       document.getElementById("head")[0].innerHTML = "<script type='text/javascript' src='./Scripts/jquery.easing.1.1.js'></script><script type='text/javascript' src='./Scripts/jquery.preloader.js'>";
   }
​​</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​
</head>

2 个答案:

答案 0 :(得分:1)

关闭脚本标记很可能被浏览器解释为当前脚本块的结尾,即使它们位于字符串文字内,这反过来意味着特定的字符串文字未被终止。

在字符串中,而不是

'</script>'

你可以做到

'<\/script>'

(当JS运行反斜杠时会被忽略。)或者有时你会看到类似的东西:

'<' + '/script>'

或者用等效的字符代码替换<

'\x3C/script>'

答案 1 :(得分:0)

试试这个,使用以下功能获取浏览器版本号

   function getInternetExplorerVersion()
    // Returns the version of Internet Explorer or a -1
    // (indicating the use of another browser).
    {
        var rv = -1; // Return value assumes failure.
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null)
                rv = parseFloat(RegExp.$1);
        }
        return rv;
    }

及以下获取浏览器名称

var browser_type = navigator.appName(“Microsoft Internet Explorer”)

所以你的新功能看起来像

   if (getInternetExplorerVersion() < 9.0 && browser_type  = "Microsoft Internet Explorer") {
    document.getElementsByTagName("head")[0].innerHTML = "<script  type='text/javascript' src='./Scripts/jquery.easing.1.1.js'></script><script type='text/javascript' src='./Scripts/jquery.preloader.js'></script><script type='text/javascript'  src='./Scripts/jquery.lavalamp.js'></script><script type='text/javascript' src='./Scripts/lavalamp-config.js'></script>";
 }  else {
       document.getElementById("head")[0].innerHTML = "<script type='text/javascript' src='./Scripts/jquery.easing.1.1.js'></script><script type='text/javascript' src='./Scripts/jquery.preloader.js'>";
   }