ajax加载html文件并执行其中的js代码,加载css样式_html/css_WEB-ITnose

WBOY
Release: 2016-06-21 09:14:49
Original
1462 people have browsed it

    function callback(res){

        var node = document.createElement('div');

        node.innerHTML = res;

        document.body.appendChild(node);

        var scriptNodes = node.getElementsByTagName('script');

        var len = scriptNodes.length;

        var styleNodes = node.getElementsByTagName('style');

        var len1 = scriptNodes.length;

        for(var i=0; i

            var scriptNode = document.createElement('script');

            if(scriptNodes[0].getAttribute('src')!='' && scriptNodes[0].getAttribute('src')!=null){

                scriptNode.setAttribute('src',scriptNodes[0].getAttribute('src'));

            }else{

                scriptNode.innerHTML = scriptNodes[0].innerHTML;

            }

            document.body.appendChild(scriptNode);

            node.removeChild(scriptNodes[0]);

        }

        for(var i=0; i

            var styleNode = document.createElement('style');

            if(styleNodes[0].getAttribute('src')!='' && styleNodes[0].getAttribute('src')!=null){

                styleNode.setAttribute('src',styleNodes[0].getAttribute('src'));

            }else{

                styleNode.innerHTML = styleNodes[0].innerHTML;

            }

            document.head.appendChild(styleNode);

            node.removeChild(styleNodes[0]);

        }

    }

    var xhr = null;

    if(window.XMLHttpRequest){

        xhr = new XMLHttpRequest();

    }else if(window.ActiveXObject){

        xhr = new ActiveXObject("Microsoft.XMLHTTP")

    }

    xhr.onreadystatechange = function(){

        if(xhr.readyState == 4){

            if(xhr.status == 200){

                callback(xhr.responseText);

            }

        }

    }

    xhr.open('get', 'main.html');

    xhr.send(null);


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template