Javascript appendChild不工作并显示错误

Javascript appendChild不工作并显示错误,javascript,Javascript,我有一个js,它在javascript控制台中显示错误,无法在此文件中调用null的“appendChild”方法: (function(d) { if( typeof jQuery === 'undefined' ) { var srsr = d.createElement('script'); srsr.src = '//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js';

我有一个js,它在javascript控制台中显示错误,无法在此文件中调用null的“appendChild”方法:

(function(d) {
    if( typeof jQuery === 'undefined' ) {
        var srsr = d.createElement('script');
        srsr.src = '//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js';
        d.body.appendChild(srsr);

        setTimeout(function(){ doThat(); }, 5000);
    } else {
        doThat();
    }


    function getHost( url ) {
        var a = document.createElement('a');
        a.href = url;
        var host = a.hostname;

        var t = host.split(".");
        if( t.length == 2 ) {
            var host = "www."+host;
        } 

        return host;
    }   

    function doThat() {
        $.ajax({
            url:    'http://mobilesplashpro.com/app/assets/php/tRaCk_loAdInG_PoPUp.php',
            type: 'GET',
            data: 'adrs='+getHost( document.domain ),
            dataType:   'jsonp',
            jsonp:  false,
            jsonpCallback: 'methodCallback',
            success: function( data ) {
                if( data.message == "yes" ) {
                    $.getScript("http://mobilesplashpro.com/app/assets/js/popup/PoPUp_txt_CoDE.js",function() { iPhoneAlert(); });
                } else {

                }
            }, 
            error: function( error ) {
                console.log( error ); 
            }
        });
    }

})( document );
在另一个文件中使用了Appendchild代码,该文件工作正常,但不在其中


谢谢

首先,将此代码放在身体的末端如果它在你的头脑中,身体不存在,如果你必须将它放在头脑中,你应该做一些间隔,直到你模拟一个DomReady事件,保证你有一个document.body要遍历

例如:

var intervalDomReady = setInterval(function() { 
    if (document.body) {     
        clearInterval(intervalDomReady);
        intervalDomReady =null;
        DomReady(); 
    }
},500);

function DomReady() { /*you code*/ }
加载jquery后,您只能从google CDN获得对它的引用。因此,您不能确定它是否会在5秒内发生,必须首先绑定到脚本的onload事件,然后尝试使用jquery函数

解决方案示例:

var d = document;
var srsr = d.createElement('script');
srsr.src = '//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js';
srsr.src.onload = function() { doThat(); } 

因为这是在加载文档时运行的,所以它是document.write的有效用例

如果使用document.write加载脚本,它还将简化工作,因为它将同步加载脚本

因此,您无需使用doThat函数和setTimeout

因此,首先将jQuery检查和脚本加载代码放在顶部一个单独的脚本中

<script type="text/javascript">
if( typeof jQuery === 'undefined' ) {
    document.write('<scr' + 'ipt src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"><\/scr' + 'ipt>');
}
</script>
然后把你的主脚本放在一个单独的脚本下面的上面一个

<script>
(function(d) {
    function getHost( url ) {
        var a = document.createElement('a');
        a.href = url;
        var host = a.hostname;

        var t = host.split(".");
        if( t.length == 2 ) {
            var host = "www."+host;
        } 
        return host;
    }
    $.ajax({
        url:    'http://mobilesplashpro.com/app/assets/php/tRaCk_loAdInG_PoPUp.php',
        type: 'GET',
        data: 'adrs='+getHost( document.domain ),
        dataType:   'jsonp',
        jsonp:  false,
        jsonpCallback: 'methodCallback',
        success: function( data ) {
            if( data.message == "yes" ) {
                $.getScript("http://mobilesplashpro.com/app/assets/js/popup/PoPUp_txt_CoDE.js",function() { iPhoneAlert(); });
            } else {

            }
        }, 
        error: function( error ) {
            console.log( error ); 
        }
    });
})( document );
</script>

现在,如果没有加载jQuery,新脚本将被写入并加载到第一个脚本的下方,第二个脚本的上方,并且我们不必执行任何类型的DOM选择。

检查d中得到了什么,看起来d.body为空。您在哪个浏览器中运行此脚本?要工作,此代码段应该放在正文中,如果它在头部,执行代码时,主体不存在。@Teemu:或者另一种解决方案可能是使用d.head。谢谢@Teemu,它工作得很好。。但不确定为什么它适用于head中的另一个文件该函数是用document而不是iframe调用的。链接的JS文件中显示了这一点。但是我在另一个文件中使用了appendchild函数,它工作得非常好。文档中的观点很好。写同步方法,我甚至不知道。如果你已经有了document.body html,这会取代我们所有的html吗?@Roger:不会。如果在初始页面加载期间使用document.write,那么html会在当前脚本之后内联编写,这会提醒我在回答中出现错误。使用加载的脚本的代码需要位于单独的脚本中,以便可以首先编写加载的脚本。我会更新的。