简体   繁体   English

防止从脚本加载图像

[英]Preventing images from loading from script

I've got a slight problem with some image tags. 我在使用某些图片标签时遇到了一个小问题。 We have a service (legacy) that returns a bunch of HTML. 我们有一个返回一堆HTML的服务(旧版)。 The image tags here contain relative paths in their HTML, which is relative to the wrong base. 此处的图片标签在HTML中包含相对路径,相对于错误的基数。 It's simple enough for me to fix this up- just bung it in a div, select the images, and fiddle their URL. 对我来说,修复此问题很简单-只需将其合并到div中,选择图像,然后摆弄它们的URL。

The problem is that as soon as you put that HTML in the div, the browser requests the incorrect URL. 问题在于,只要将该HTML放入div,浏览器就会请求不正确的URL。 So the console jams up with 404 warnings for the images, even though the whole point of putting them in the div is to correct the problem. 因此,即使将图像放入div的全部目的是要纠正问题,控制台也会为图像显示404警告。

I've seen that you can prevent the image from loading with a noscript tag. 我已经看到,您可以使用noscript标签阻止图像加载。 Unfortunately, when replacing the div with a noscript, we've had a fairly big problem- it seems that manipulating noscript tags from script is, well, very difficult. 不幸的是,当用noscript替换div时,我们遇到了一个很大的问题-从脚本中操作noscript标签似乎非常困难。 The native functions (in Chrome) seem to return completely different results. 本机函数(在Chrome中)似乎返回完全不同的结果。 For example, when we try to set the inner HTML of the noscript, it seems to think that we actually meant to set that HTML as the text. 例如,当我们尝试设置noscript的内部HTML时,似乎认为实际上是要将HTML设置为文本。

How can I prevent the image tags from loading their URLs until after I have fixed them up? 在修复图像标签之前,如何防止它们加载它们的URL?

Edit: I have the HTML back as a string from an AJAX request. 编辑:我有HTML回作为AJAX请求中的字符串。

This might/might not solve your particluar issue as I don't have any code to evaluate. 这可能/可能无法解决您的特殊问题,因为我没有任何代码可以评估。 This methods have been tested successfully (no console errors) on Chrome/Edge/FF. 此方法已在Chrome / Edge / FF上成功测试(没有控制台错误)。

Based on your edit where AJAX is used, adding a html string using script: 根据使用AJAX的编辑,使用脚本添加html字符串:

var el = document.createElement("DIV");
el.innerHTML = '<img src="wrong_path.jpg" />';
var imgs = el.getElementsByTagName("IMG");
imgs[0].src = "http://placehold.it/350x150";
document.body.appendChild(el);

Another way that might could solve it could be a DOM parser, to convert a string to HTML, iterate the images and update their "src" and return a subset of the DOM to be inserted into the existing DOM. 可能解决该问题的另一种方法可能是DOM解析器,将字符串转换为HTML,迭代图像并更新其“ src”,然后返回DOM的子集以插入到现有DOM中。

Src: https://developer.mozilla.org/en-US/docs/Web/API/DOMParser Src: https//developer.mozilla.org/zh-CN/docs/Web/API/DOMParser

If one need to catch an insert to manually inspect the element inserted, DOM node inserts could be used: 如果需要捕获一个插入以手动检查插入的元素,则可以使用DOM节点插入:

Src: https://stackoverflow.com/a/18152595/2827823 Src: https//stackoverflow.com/a/18152595/2827823

document.body.addEventListener("DOMNodeInserted", function (evt) {
    if (evt.target.tagName == "IMG") {        
        evt.target.src = "http://placehold.it/350x150";
    }
});

document.body.innerHTML = '<img id="foo" src="wrong_path_bar.png"/>';

If one need to catch these generated errors without manually look for them, an error event handler could be used: 如果需要捕获这些生成的错误而无需手动查找它们,则可以使用错误事件处理程序:

Src: Alter the prototype of an image tag? Src: 更改图像标签的原型吗?

window.addEventListener("error", function(e) {
    if ( e && e.target && e.target.nodeName && e.target.nodeName.toLowerCase() == "img" ) {
        // Bad image src
        // e.target.src = "Do an url replacement";
    }
}, true);

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

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