是否可以动态替换html页面的脚本部分

Is it possible to dynamically replace the script section of an html page?

本文关键字:脚本部 html 动态 替换 是否      更新时间:2023-09-26

我正在开始一个页面,其中一些罐装/模拟数据被馈送到Chart.JS图表,例如:

data: [2755, 2256, 1637, 1608, 1603, 1433, 1207, 1076, 1056, 1048],

我想用真实的/真实的数据替换那些伪造的数据。我可以用AJAX调用返回的内容替换html页面的<script>部分吗?

IOW,我能做这样的事情吗:

                    $.ajax({
                        type: 'GET',
                        url:
                            '@Url.RouteUrl(routeName: "QuadrantData"
routeValues: new { httpRoute = true, unit = "un", begdate = "bd", enddate 
"ed" })'
                    .replace("un", encodeURIComponent(_unit))
                    .replace("bd", encodeURIComponent(_begdate))
                    .replace("ed", encodeURIComponent(_enddate)),
                        contentType: 'text/plain',
                        cache: false,
                        xhrFields: {
                            withCredentials: false
                        },
                        success: function (returneddata) {
                            $("script").html(returneddata);
                            $("#newhourglass").addClass("hide");
                        },
                        error: function () {
                            console.log('error in ajax call t
QuadrantData');
                            $("#newhourglass").addClass("hide");
                        }
                    });

IOW,而不是:

$("html").html(returneddata);

或者类似的东西:

$("script").html(returneddata);

我将补充Travis和其他评论者所说的内容。

解析HTML时,一旦看到关闭的script标记,就会执行script标记。剩下的只是一个带有内部文本的标签。我将在下面的片段中演示这种行为。

script {
  display: block;
  background: white;
  min-height: 100px;
  border: 1px solid black;
  white-space: pre;
  font-size: 13px;
}
Below scripts counts upward every second. But editing it wont change its after effects.
<script contenteditable="true">
counter=0;
counterInterval = setInterval(function clicked(){
  console.log(counter++);
},1000);
</script>

此代码段具有contenteditable script。您可以在更改文本框时更改其内容。但你会看到它的效果不会改变。

或者,当新的script标签被插入到页面中(例如,具有javascript(时,它被执行。下面的片段演示了这一点。

reinsertScripts = function() {
  var script = document.querySelector("#reinsert");
  var parent = script.parentNode;
  var nscript = document.createElement("script");
  nscript.contentEditable = true;
  nscript.id = "reinsert";
  nscript.innerHTML = script.innerHTML;
  parent.removeChild(script);
  parent.appendChild(nscript);
}
script[contenteditable="true"] {
  display: block;
  background: white;
  min-height: 100px;
  border: 1px solid black;
  white-space: pre;
  font-size: 13px;
}
<p><span>Count: </span><span id="counter"></span></p>
Below scripts counts upward every second. Clicking reinsert button will rerun the script and you may see changes.
<script contenteditable="true" id="reinsert">
  counter = 0;
  clearInterval(window.counterInterval);
  counterInterval = setInterval(function clicked() {
    document.getElementById("counter").innerHTML=counter++;
  }, 1000);
</script>
<button onClick="reinsertScripts()">Reinsert script</button>

虽然这是你问题的答案,但我相信这不是你需要的答案。查找"动态更改的ChartJS数据"。基本上,您可以使用chart.addDatachart.removeData函数来更新图表的数据。

我可以用AJAX调用返回的内容替换html页面的[script]部分吗?

不,你不能。一旦浏览器对脚本进行了解释,它就只是display: none;的文本。事实上,您可以删除所有脚本$('script').remove(),而不会对页面本身产生任何影响。

这是因为一旦脚本执行完毕,环境就会根据代码的执行情况进行更改,并反映执行代码的更改。

相反,您应该尝试更改已分配给的data变量,并使用该新数据重新初始化图表。或者,你可以在新数据到来时添加新的图表。无论哪种方式,让数据从ajax成功中显示出来的过程都需要在代码中完成,而不是简单地替换脚本标记的html。

为了执行ajax调用返回的任何脚本,您可以在页面上的元素上使用.html(htmlWithScriptElements),jQuery将在内部为您执行脚本(使用eval(。