首页 > 解决方案 > php 包括,通过 javascript 替代 - 网络和科尔多瓦测试

问题描述

为我和后代保存

这是通过 javascript 创建的替代包含。我在cordova(网络和安卓)上进行了测试和工作。

    function jsinclude()
    {
        let x, i, el, file, request;

        /*loop all doc el*/
        x = document.getElementsByTagName("*");
        for (i = 0; i < x.length; i++)
        {

            el = x[i];                          //el by el
            file = el.getAttribute("include");  //get path

            if (file)
            {
                //check the path error
                if (file.startsWith("/") )
                { 
                    alert("\n\nerror in file path: do not use the ' / ' as initial.\n\n\Follow the example below:\n<div include='myfolder/.../file.html'></div>\n\n");
                    let mex = '<p style="backraound:white !important; padding:20px !important; border: 1px solid red !important; color:red !important; font-weight:bold !important;"> !! ERROR: WRONG PATH FOR INCLUDE</p>';
                    el.innerHTML = mex;
                };

                //create HTTP request
                request = new XMLHttpRequest();
                request.onload = function ()
                //request.onreadystatechange = function ()  //simple alternative
                {

                    if (this.status == 200 ) // if ok
                    {
                        console.log("include-js: inlcude file: " + file +" | rquest status: "+this.status+" | data printed: "+this.responseText);
                        el.innerHTML = this.responseText;
                    }
                    else if (this.status == 404) // if ok... but not found
                    {
                        alert("error in file path or server:\n404 file not founded.");
                    }
                    else if (this.status != (404 && 0 && 200)) // if... wtf!??
                    {
                        alert("undefined server error on include js");
                    }

                    // stop and repeat
                    el.removeAttribute("include");
                    jsinclude();

                }

                // sand all... and exit
                request.open("GET", file, true);
                request.send();
                return;
            }
        }

};

身体内的每个标签都可以完美运行。

<body>
    <div include="yourpath/test.html">
        included html content ;)
    </div>
</body>

阅读注释以包含头部内容

<head include="yourpath/head meta etc.html">

    content loaded... (read note)

</head>

注意:我还没有测试,在包含头部之后,它是否会继续阻止头部标签内的其他脚本(目前)。

标签: javascriptcordovainclude

解决方案


推荐阅读