一个给定的URL和地点的可加载HTML内容完全没有(如文件撰写())在JavaScript加载、地点、文件、内容

2023-09-10 20:28:30 作者:Autism 孤独症゛

我想c写一个给定的URL的负载HTML内容的JavaScript的$ C $,然后将加载的HTML code究竟在何处脚本放置。 (也许这看起来像iframe标签的功能,但我不希望iframe标签成为网上平台。我只是想加载HTML code和将它放在这里不添加任何容器或额外父) 是这样的:

I want to write a JavaScript code that load HTML contents of a given URL and place the loaded HTML code exactly where the script is placed. (maybe this looks like functionality of the iframe tag, but i dont want "iframe tag" become a medium. i just want to load the html code and place it there without adding any container or extra parent) something like this:

var url = "http://example.com/";    
var html = loadhtmlcontents(url); // something like simplexml_load_file($url) for php and xml
document.write(html); // somthing like saveHTML() in DOMDocument class of php

我已经试过AJAX方法,但它不工作:

I've tried AJAX approach but it doesn't work:

    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.wirte( xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", Url, false );
    xmlhttp.send();

请你给我一个同等或更正这些? (纯JS的办法是在这种情况下pferred jQuery的我$ P $)

could you please give me an equivalent or correction to these? (pure JS approach is preferred to JQuery for me in this situation)

推荐答案

获取电流剧本标记是可能的,但这里的另一种方法(记住,它取代了与 ID 标签的整个元素):

Getting the current script tag is possible, but here's another approach (keep in mind it replaces the entire element with the id tag):

标签:

<script id='test'>docWrite('test', '/echo/html')</script>

Javascript的声明一个新的功能:

Javascript declaring a new function:

function docWrite(id, url) {
    var xmlhttp = new XMLHttpRequest(),
        _id = id;

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState == 4 
             && xmlhttp.status == 200) {
            var el = document.getElementById(_id),
                textnode = el.firstChild,
                div = document.createElement('div');

            div.id = _id;
            div.appendChild(textnode);

            el.parentNode.insertBefore(div, el.nextSibling);
            el.parentNode.removeChild(el);

            console.log(xmlhttp.responseText);
        }
    }

    xmlhttp.open("GET", url, false );
    xmlhttp.send();
}

http://jsfiddle.net/dpgk1Lx2/

在这里,所有我做的是复制 ID - 相关标签(有之内容概不的responseText 显示)。在您的使用,你可以这样做,而不是:

Here, all I'm doing is copying the contents of the id-related tag (there is no responseText to display). In your usage, you would do this instead:

function docWrite(id, url) {       
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState == 4 
             && xmlhttp.status == 200) {
            var el = document.getElementById(_id)
                div = document.createElement('div');

            div.id = _id;
            div.appendChild(xmlhttp.responseText);

            el.parentNode.insertBefore(div, el.nextSibling);
            el.parentNode.removeChild(el);
        }
    }

    xmlhttp.open("GET", url, false );
    xmlhttp.send();
}