简体   繁体   English

javascript函数从其他文件加载内容的问题

[英]problems with javascript function loading content from other files

Basically I'm trying to build a functionality in which I only really edit my index.php, I got a lot of other php files with just a form in them or just a few lines of text. 基本上,我正在尝试构建仅真正编辑index.php的功能,我得到了很多其他php文件,它们中只有一种形式或只有几行文本。
What I want to achieve is to load these other files in the contentwrapper of my index.php. 我要实现的是将这些其他文件加载到index.php的contentwrapper中。

I have been successfull on doing this with an iframe and with a html <object> . 我已经成功地使用iframe和html <object>做到了这一点。
The problem with these though is that first of all they load an all new #document in the DOM, and also my webpage has no set height so height: 100% won't work on those and I would get these ugly scrollbars and stuff. 这些的问题是,首先它们在DOM中加载了所有新的#document ,而且我的网页没有设置高度,所以height: 100%不能在这些height: 100%工作,而我会得到这些丑陋的滚动条和其他东西。

after searching a lot on SO today I found a few interesting solutions which I combined, this is what I'm trying now: 今天在SO上进行了大量搜索之后,我发现了一些有趣的解决方案,将它们组合在一起,这就是我现在正在尝试的方法:

<script type="text/javascript" href="js/csi.min.js"></script>
<script type="text/javascript">

function load_content(target){
    document.getElementById('contentwrapper').innerHTML='<div data-include="' + target + '" ></div>';
    return false;
}

</script>

now you may question what data-include is, this is a very nice workaround I found on SO. 现在您可能会质疑data-include什么data-include ,这是我在SO上找到的一个非常好的解决方法。

THIS is what it does , it basically calls a .js file that replaces the containing element with the data that is in the file (target in the above example) 这就是它的作用 ,它基本上调用一个.js文件,该文件用文件中的数据替换包含元素(target in the above example)

I call this functionality like this: 我称这种功能为:

<a href="#" onclick="load_content('update.php');">Update profile</a>

It works as far as adding this to the DOM: 它可以将其添加到DOM:

<div id="contentwrapper">
    <div data-include="update.php" ></div>
</div>

but besides that it does nothing, I think that it doesn't call the .js file for the data-include attribute. 但是除了它什么都不做之外,我认为它不会为data-include属性调用.js文件。 But I can't find a solution for this nowhere. 但我无处可寻。

(BTW: the data-include attribute does work if I put it in a tag manually without javascript) (顺便说一句:如果我将其手动放入标签中而不使用javascript,则data-include属性确实可以工作)

I Hope I didn't overexplain the situation, and I thank everyone that tries to help in advance! 希望我不会对此情况有过多的解释,并且感谢所有尝试提前提供帮助的人!

The csi.js script is only run once after the page is loaded. 页面加载后, csi.js脚本仅运行一次。 It just goes over all the elements with the data-include attribute and runs the fragment function. 它仅遍历具有data-include属性的所有元素,并运行fragment函数。

<script type="text/javascript">
function fragment(el, url) {
    var localTest = /^(?:file):/,
        xmlhttp = new XMLHttpRequest(),
        status = 0;

    xmlhttp.onreadystatechange = function() {
        /* if we are on a local protocol, and we have response text, we'll assume
         * things were sucessful */
        if (xmlhttp.readyState == 4) {
            status = xmlhttp.status;
        }
        if (localTest.test(location.href) && xmlhttp.responseText) {
            status = 200;
        }
        if (xmlhttp.readyState == 4 && status == 200) {
            el.outerHTML = xmlhttp.responseText;
        }
    }

    try { 
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
    } catch(err) {
        /* todo catch error */
    }
}

function load_content(target){
    fragment(document.getElementById('contentwrapper'), target);
    return false;
}
</script>

Then call it like this: 然后这样称呼它:

<a href="#" onclick="load_content('update.php');">Update profile</a>

So, the only thing you need is to call this function for the new created element. 因此,您唯一需要的就是为新创建的元素调用此函数。 Pass the DOM element and the url to this function and it will take care of loading the contents of the requested resource in the corresponding element. 将DOM元素和url传递给此函数,它将负责将请求的资源的内容加载到相应的元素中。

May we assume that you followed this advise from the repository: The only caveat is Chrome, which restricts access to local files via AJAX. 我们可以假设您从存储库中遵循了此建议: 唯一的警告是Chrome,它限制了通过AJAX访问本地文件。 To resolve this, simply add --allow-file-access-from-files to your Chrome runtime. 要解决此问题,只需将--allow-file-access-from-files添加到您的Chrome运行时中即可。

If you didn't, and you're using Chrome, then this stands out to me, and you didn't indicate that you'd corrected the security block that Chrome puts in place. 如果您没有这样做,而您正在使用Chrome,那么这对我来说很突出,并且您没有表示已更正了Chrome设置的安全保护块。

The csi.js only runs on window.onload. csi.js仅在window.onload上运行。 Try 尝试

<a href="#" onclick="function() {load_content('update.php'); window.onload(); }">
Update profile</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM