0

I have the following code:

        <script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script>
        function googleJS1(){
            var iframe = document.getElementsByTagName('iframe')[0];
            var doc = iframe.contentWindow.document;
            var newScript = doc.createElement('div');
            newScript.setAttribute("id", "google_translate_element");
            var bodyClass = doc.getElementsByTagName('body')[0];         
            bodyClass.insertBefore(newScript, bodyClass.childNodes[0]);
        }

        function googleJS2(){
            var iframe = document.getElementsByTagName('iframe')[0];
            var doc = iframe.contentWindow.document;
            var newScript = doc.createElement('script');
            newScript.setAttribute("src", "http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit");
            var bodyClass = doc.getElementsByTagName('head')[0];         
            bodyClass.insertBefore(newScript, bodyClass.childNodes[0]);
        }

        function googleJS3(){
            var iframe = document.getElementsByTagName('iframe')[0];
            var doc = iframe.contentWindow.document;
            var newScript = doc.createElement('script');
            newScript.setAttribute("src", "http://www.mydomain.com/google.js");
            var bodyClass = doc.getElementsByTagName('head')[0];         
            bodyClass.insertBefore(newScript, bodyClass.childNodes[1]);
        }

        function googleJS4(){
            var iframe = document.getElementsByTagName('iframe')[0];
            var doc = iframe.contentWindow.document;
            var newScript = doc.createElement('style');
            var content = doc.createTextNode('.goog-te-banner-frame { display: none; } #google_translate_element {}');
            newScript.appendChild(content);
            var bodyClass = doc.getElementsByTagName('head')[0];         
            bodyClass.insertBefore(newScript, bodyClass.childNodes[2]);
        }
        </script>
        <iframe width=100% height= 100% onload ="googleJS1(); googleJS2(); googleJS3(); googleJS4();" class=iframe2 src="www.mydomain.com/test.html">

This code works fine on another server of mine elsewhere. However whenever this is run the console says "Uncaught referencer error, method undefined". Even though it is defined, why does it throw this message and how do I solve it?

Thanks

4

1 に答える 1

0

I haven't tested, but I'm assuming this is because the onload event is attached to the iframe, and will therefore be called inside the iframe's scope. Try calling window.parent.googleJS1() instead.

于 2013-02-26T10:51:00.497 に答える