appendChild不工作并显示错误

appendChild not working and showing error

本文关键字:显示 错误 工作 appendChild      更新时间:2023-09-26

我有一个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事件(这意味着你有一个文档.body要遍历)。

示例:

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

一旦加载jquery,您就只能从谷歌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选择。