简体   繁体   English

如何使用 ajax 和纯 javascript 加载 HTML(其中包含 javascript)内容

[英]How to Load HTML(with javascript in it) content using ajax and pure javascript

I have been working on a project that uses the 'one page app' concept: All the pages are loaded using jQuery and ajax, something like this :我一直在研究一个使用“单页应用程序”概念的项目:所有页面都使用 jQuery 和 ajax 加载,如下所示:

$.ajax({
    url : some_url
    ,data: some_data
    ,success : function(resp){
        $("#somediv").html( resp.view ) 
    }  
});

And like this, it has been working fine .像这样,它一直运行良好。 All the HTML, CSS and JS included on "resp.view" behave like a charm. “resp.view”中包含的所有 HTML、CSS 和 JS 表现得像一个魅力。

But, one day I needed to do it without jQuery :但是,有一天我需要在没有 jQuery 的情况下做到这一点:

var xhr = new XMLHttpRequest();
xhr.open("POST",  someurl);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
        var data = JSON.parse(xhr.responseText);

        somediv.innerHTML = data.view;

    }
};

xhr.send(some_data);

The pure JS way works fine to load only HTML and CSS include on "data.view" ... but, it does not execute any javascript included in the HTML returned ...纯 JS 方式可以很好地仅加载 HTML 和 CSS 包含在“data.view”上……但是,它不执行返回的 HTML 中包含的任何 javascript……

I could use document.head.append to add all the scripts I need, but, there is some JS code into the html ... and, I deal with lots of different pages as reponse ... so, it is not an option to add the dependencies manually.我可以使用document.head.append添加我需要的所有脚本,但是,html 中有一些 JS 代码......而且,我处理了许多不同的页面作为响应......所以,这不是一个选择手动添加依赖项。

So:所以:

How can I insert an string, that contains a HTML page with styles and scripts, to my current page and get it to behave such a html page ?如何将包含带有样式和脚本的 HTML 页面的字符串插入到我的当前页面并使其表现出这样的 html 页面? how the jQuery does that ? jQuery 是如何做到的?

.... Scope .... 范围

I am working now in a script that injects some html codes into my client's website.我现在正在一个脚本中工作,该脚本将一些 html 代码注入我客户的网站。 And, at the moment I call my script and invoke an ajax to request the page, I can not be sure if jQuery is loaded on DOM.而且,在我调用脚本并调用 ajax 来请求页面的那一刻,我无法确定 jQuery 是否已加载到 DOM 上。 That's why, in this case, I need pure JS.这就是为什么,在这种情况下,我需要纯 JS。 Then, the HTML returned has the jQuery source and others.然后,返回的 HTML 包含 jQuery 源代码和其他内容。

The workaround I found to deal with this matter was to load my local jQuery source into DOM beforehand, and then, use jQuery ajax to do the job as always.我发现处理这个问题的解决方法是预先将我的本地 jQuery 源加载到 DOM 中,然后像往常一样使用 jQuery ajax 来完成这项工作。 it works, but, I got curious how so !它有效,但是,我很好奇怎么会这样!

For JS loaded via AJAX, you could do:对于通过 AJAX 加载的 JS,您可以执行以下操作:

somediv.innerHTML = data.view;
somediv.querySelectorAll('script').forEach(function(script) {
    var el = document.createElement('script');

    if (script.src) {
        el.setAttribute('src', script.src)
    } else {
        el.innerHTML = script.innerHTML;
    }

    script.replaceWith(el);
});

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

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