javascript - 如何在纯 JavaScript 的外部 HTML/SVG 文件中执行 javascript?
问题描述
我有一个 SVG 文件,它定义了它的一些样式并<defs>
使用 JavaScript 动态地定义。
我在 HTML 文件中使用所述 SVG 文件。
- 如果我自己打开 SVG 文件,它的 JavaScript 就会被执行。
- 如果使用 jQuery,我通过 AJAX 将 SVG 文件包含到 HTML 文件中(将 SVG 附加到 HTML 文档),那么 SVG 的 JavaScript 也会被执行。
- 但是,使用纯 JavaScript:如果我通过 AJAX 将 SVG 文件包含到 HTML 文件中(将 SVG 附加到 HTML 文档中),那么 SVG 的 JavaScript 不会被执行。
我正在尝试理解并修复这种行为。
作为 MCVE:
ajax-callee.svg
:
<?xml version="1.0" encoding="UTF-8"?>
<svg version="1.1"
baseProfile="full"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ev="http://www.w3.org/2001/xml-events"
>
<script><![CDATA[
console.log( 'ajax-callee.svg › script tag' );
/** When the document is ready, this self-executing function will be run. **/
(function() {
console.log( 'ajax-callee.svg › script tag › self-executing function' );
})(); /* END (anonymous function) */
]]></script>
</svg>
ajax-caller-jquery-withSVG.html
(工作正常):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>(Check out the console.)</p>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$( document ).ready(function() {
$.ajax({
method: "GET",
url: "ajax-callee.svg",
dataType: "html"
}).done(function( html ) {
/** Loading the external file is not enough to have, it has to be written to the doc too for the JS to be run. **/
$( "body" ).append( html );
});
});
</script>
</body>
</html>
ajax-caller-pureJS-withSVG-notworking.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>(Check out the console.)</p>
<script>
/** AJAX CALLER **/
/** When the document is ready, this self-executing function will be run. **/
(function() {
var ajax = new XMLHttpRequest();
ajax.open("POST", "ajax-callee.svg", true);
ajax.send();
/**
* Append the external SVG to this file.
* Gets appended okay…
* …but its JavaScript won't get executed.
*/
ajax.onload = function(e) {
/** Parse the response and append it **/
var parser = new DOMParser();
var ajaxdoc = parser.parseFromString( ajax.responseText, "image/svg+xml" );
document.getElementsByTagName('body')[0].appendChild( ajaxdoc.getElementsByTagName('svg')[0] );
}
})(); /* END (anonymous function) */
</script>
</body>
</html>
- jQuery 有什么不同导致 JavaScript 被执行?
- 我该怎么做才能运行包含的 SVG JS?
- 有什么要注意的吗?
仅供参考,我正在尝试使用 SVG,但在我的测试中,当为被调用者使用 HTML 文件时,行为是相同的。
解决方案
从使用 DOMParser 解析的 HTML 规范脚本不会执行 https://html.spec.whatwg.org/multipage/scripting.html#script-processing-noscript
禁用脚本定义意味着以下脚本将不会执行:XMLHttpRequest 的 responseXML 文档中的脚本、DOMParser 创建的文档中的脚本、由 XSLTProcessor 的 transformToDocument 功能创建的文档中的脚本以及首先由脚本插入的脚本到使用 createDocument() API 创建的 Document 中。[XHR] [DOMPARSING] [XSLTP] [DOM]
看起来 jQuery 正在获取文本内容并创建一个新的脚本标签并将其附加到文档中。
http://code.jquery.com/jquery-3.3.1.js
DOMEval( node.textContent.replace( rcleanScript, "" ), doc, node );
function DOMEval( code, doc, node ) {
doc = doc || document;
var i,
script = doc.createElement( "script" );
script.text = code;
if ( node ) {
for ( i in preservedScriptAttributes ) {
if ( node[ i ] ) {
script[ i ] = node[ i ];
}
}
}
doc.head.appendChild( script ).parentNode.removeChild( script );
}
因此,对于您的代码,您可以执行相同的操作:
ajaxdoc.querySelectorAll("script").forEach((scriptElement) => {
let script = document.createElement("script");
script.text = scriptElement.textContent;
document.head.appendChild(script)
});
推荐阅读
- java - 缺少来自数据库驱动的消息 bean 的默认 Spring 消息
- openlayers - Openlayers:从请求中加载坐标数据
- delphi - Delphi,在 where 条件中带有 DetailFields 的 Master-Detail(不在选择部分中)
- azure-timeseries-insights - Azure 时序见解的底层数据库/存储是什么?
- java - Java/Android:在数组中解析没有单个字符串的 JSON
- react-native - 在没有 aws amplify 的情况下在博览会 ImagePicker 中上传图像
- javascript - javascript文件的浏览器缓存失效而不更改src URL
- spring-data-jpa - Spring with Querydsl:java.lang.ClassCastException
- r - 使用 ggplotly 的地图显示不正确(R)
- excel - 选中一个框会取消选中 Excel 中的相邻框