0

我有以下代码:

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

这段代码在我其他地方的另一台服务器上运行良好。但是,无论何时运行,控制台都会显示“未捕获的引用错误,方法未定义”。即使它已定义,为什么它会抛出此消息,我该如何解决?

谢谢

4

1 回答 1

0

我没有测试过,但我假设这是因为onload事件附加到 iframe,因此将在 iframe 的范围内调用。试着打电话window.parent.googleJS1()

于 2013-02-26T10:51:00.497 回答