简体   繁体   中英

Why does this JavaScript addle Chrome?

I load several pages per JavaScript into an site of ancient history with iFrames.

There is a function which checks how height the iFrame content is and then stretches the iFrame to this height, too.

function dynamisch()
{

    //bla bla bla, script that is executed well

    // HERE IS THE ERROR LINE:
    var h = parent.frames["content"].document.getElementsByTagName("body")[0].scrollHeight + 20;

    //bla bla bla, again ancient typings which is interpreted as script
}

This executed well on every page. On the content pages, there is also a JavaScript that checks for the browser name and then includes the fitting CSS file. Don't judge me yet, this is living history!

So before the ages of Chrome, there was this CSS-including-script without Chrome. As Chrome began to rise, the script has been adapted to also include Chrome. With the OLD script, the snippet above executes well - no problems. With the NEW script, it outputs the following error on the marked error line:

Uncaught TypeError: Cannot read property 'document' of undefined
dynamisch
onload

So why does the NEW script (see below) irritate Chrome that much that it doesn't remember what document used to be?


OLD script in <head> , executes well:

if(navigator.appName == "Netscape")
    {
        document.write("<link rel='stylesheet' href='../styles/aquado_ff.css.asp' type='text/css'>");
    }
    else if(navigator.appName == "Opera")
    {
        document.write("<link rel='stylesheet' href='../styles/aquado_ff.css.asp' type='text/css'>");
    }
    else if(navigator.appName == "Microsoft Internet Explorer")
    {
        document.write("<link rel='stylesheet' href='../styles/aquado_ie.css.asp' type='text/css'>");
    }

NEW script in <head> , irritates Chrome:

var name = navigator.userAgent.toLowerCase()
    //InternetExplorer
    if(name.indexOf("msie")>-1)
    {
    document.write("<link rel='stylesheet' href='../styles/aquado_ie.css.asp' type='text/css'>");
    }
    else if (name.indexOf("chrome")>-1)
    {
    document.write("<link rel='stylesheet' href='../styles/aquado_ff.css.asp' type='text/css'>");
    }
    else if (name.indexOf("firefox")>-1)
    {
    document.write("<link rel='stylesheet' href='../styles/aquado_ff.css.asp' type='text/css'>");
    }
    else if (name.indexOf("opera")>-1)
    {
    document.write("<link rel='stylesheet' href='../styles/aquado_ff.css.asp' type='text/css'>");   
    }
    else
    {
    document.write("<link rel='stylesheet' href='../styles/aquado_ie.css.asp' type='text/css'>");
    }

Ok, I found the reason by trial and error.

See that line?

var name = navigator.userAgent.toLowerCase()

Chrome seems to have problem to adress a variable named name . I switched it to bname and now everything executes well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM