我正在使用以下代码动态添加 javascript
HtmlGenericControl scriptTagLinks = new HtmlGenericControl("script");
scriptTagLinks.Attributes["type"] = "text/javascript";
var scrip = "var aImgs=[" + appendString.ToString().TrimEnd(new char[] { ',' }) + "]";
scriptTagLinks.InnerHtml = scrip;
Javascript 正在添加到 ascx 页面。但我的问题是我在页面中有两个 javascript 块,一个是添加 dynamicllay ,即提供所有图像。另一个 javascript 如下。
<script type="text/javascript>
window.onload = function () {
for (var i = 0; i < aImgs.length; i++) {
var oImg = new Image();
oImg.src = aImgs[i];
aImages.push(oImg);
oImg.onload = function () {
textureWidth = oImg.width;
textureHeight = oImg.height;
}
}}
</script>
但在脚本下方添加了动态创建的 javascript。但应该像这样在脚本上方添加。
<script type="text/javascript">
var aImgs = [
'DesktopModules/DNAiusCubeImages/Check/pic1.jpg',
'DesktopModules/DNAiusCubeImages/Check/pic2.jpg',];
</script>
<script type="text/javascript>
window.onload = function () {
for (var i = 0; i < aImgs.length; i++) {
var oImg = new Image();
oImg.src = aImgs[i];
aImages.push(oImg);
oImg.onload = function () {
textureWidth = oImg.width;
textureHeight = oImg.height;
}
}}
</script>How can i achieve this .