Search code examples
javascriptjqueryinternet-explorergoogle-chrome-frame

Simple JavaScript / jQuery not working


I've probably missed something big, but I've been trying for hours...

So to get users of IE 8 and below to install Chrome Frame I tried the GCF Install JS files provided but I'd prefer my own implementation - the GCFInstall JS will let the popup only open once per session, even if invoked by a button the second time. The only solution I've found doesn't let you close it if you open it the second time.

So here's the code I put on my pages:

HTML:

<!--[if lt IE 9]><script type="text/javascript" src="/resources/js/getgcf.js"></script>
<div style="border:1px solid black; padding: 3px;"><img src="http://cdn.dustball.com/information.png" alt="info"> You appear to be using an older version of Internet Explorer. This website relies on technology not supported by Internet Explorer. To improve your experience (and fix layout errors):
<br><button id="gcfdl">Activate Google Chrome Frame</button><br>You won't notice anything different in the way you use the internet, except that most websites will look better.</div><![endif]-->

getgcf.js:

$("#gcfdl").click(function(){
    var wants_normal_installation = confirm("You will now be taken to google.com/chromeframe where Google Chrome Frame will be activated. Please wait about 10 seconds after clicking Accept and Install, and you will automatically be taken back to this page. Press OK for normal installation (recommended), or Cancel for single-user installation (use if you don't have administrator rights on your computer):");
    if(wants_normal_installation){
        window.location("http://www.google.com/chromeframe?redirect=true");
    }
    else{
        window.location("http://www.google.com/chromeframe?user=true&redirect=true");
    }
});

Yes - it's that simple. jQuery normally works perfectly :(

IE does show "Errors on page" but when I click it the message shows a confusing error I can't work out what it means.

I put the code into Chrome and it gave another console message I can't get my head around...

Any help would be appreciated. Thanks.


Solution

  • Wrap your code in a document ready event...

    $(document).ready(function () {
    
        $("#gcfdl").click(function(){
            var wants_normal_installation = confirm("You will now be taken to google.com/chromeframe where Google Chrome Frame will be activated. Please wait about 10 seconds after clicking Accept and Install, and you will automatically be taken back to this page. Press OK for normal installation (recommended), or Cancel for single-user installation (use if you don't have administrator rights on your computer):");
            if(wants_normal_installation){
                window.location("http://www.google.com/chromeframe?redirect=true");
            }
            else{
                window.location("http://www.google.com/chromeframe?user=true&redirect=true");
            }
        });
    
    });