[英]How should I change an injected HTML element with variable using javascript?
Question: What is the best practice to inject HTML, that contains javascript? 问题:注入HTML的最佳实践是什么,包含javascript?
Script to load content in page: 在页面中加载内容的脚本:
elmnt = document.getElementById("contentcontainer");
elmnt.innerHTML = this.responseText;
Page: 页:
Welcome to our restaurant!
<div id="contentcontainer"></div>
Responsetext: responseText的:
<div>
The menu for today:
<script> Script to get API resonse menu(today); <script>
</div>
So I'm injecting HTML that contains a script. 所以我正在注入包含脚本的HTML。 But the script is not executed.
但是脚本没有被执行。 How should I change variable HTML inside an injection?
我应该如何在注入中更改变量HTML?
您可以使用“ 模板字符串 ”并注入已处理的HTML。
I dont understand what the problem is. 我不明白问题是什么。
InnerHtml
should work perfectly, as long as the variable is global and also you get the desire content. InnerHtml
应该可以完美地工作,只要变量是全局的,你也可以获得所需的内容。
Here is an example of what i understod off what you want. 这是我想要了解你想要的东西的一个例子。
let str = '<p><script>var test="this is a test"</script></p>'
document.getElementsByTagName("BODY")[0].innerHTML =str;
console.log(test) // and here is the injected script variable result
To get the content of the site from the url you will have to use a library like jq getscript
Did i understand you correctly? 要从网址获取网站的内容,您将不得不使用像jq
getscript
这样的库我是否理解正确?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.