Why I am getting this error with the script in my website?

﹥>﹥吖頭↗ 提交于 2019-12-12 05:27:41

问题


I am trying to fix the problem that I am facing with JQuery LavaLamp Menu Bar in my website by adding the following bunch of code suggested by one of the amazing developers and people in this community:

<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>​​​​​​​​​​​​​​​​​​​​​​​​​​​

And in the Visual Studio 2010, I got the following error

Unterminated string constant

under the following line:

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>';

So how I can fix this problem?

UPDATE #1:

I updated my code to what you suggested guys and I am still getting the same error. Also, here's a snapshot of the error:

UPDATE #2 I modified my code to let the Master Page includes the following code and I am still getting the same error that shows at the top of the page as shown in the above snapshot.

<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>

回答1:


The closing script tags are likely being interpreted by the browser as the end of the current script block even though they're inside a string literal, which in turn means that particular string literal is unterminated.

Within your string, instead of

'</script>'

You can do

'<\/script>'

(When the JS runs the backslash is ignored.) Or sometimes you see something like:

'<' + '/script>'

Or replace the < with the equivalent character code:

'\x3C/script>'



回答2:


Try this, Use the below function to get the browser version number

   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;
    }

and below for getting the browser name

var browser_type = navigator.appName ("Microsoft Internet Explorer")

so your new function would look like

   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'>";
   }


来源:https://stackoverflow.com/questions/11945771/why-i-am-getting-this-error-with-the-script-in-my-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!