简体   繁体   English

我可以将页面重定向到图像以便可以在IMG标签中使用吗?

[英]Can I redirect a page to an image so it'll work in an IMG tag?

Is there any way to have a page redirect to an image file in such a way that it'll work inside an image tag? 有什么方法可以将页面重定向到图像文件,使其可以在图像标签中使用? A simple example would be something like 一个简单的例子就是

page1.html: page1.html:

 <html> <img src="page2.html"> </html> 

page2.html page2.html

 <html> <script> //redirect to dynamicallyChosenImage.png </script> </html> 

I need to be able to do this in pure HTML/Javascript if at all possible (my servers don't allow any server-side processing). 我需要能够使用纯HTML / Javascript做到这一点(我的服务器不允许任何服务器端处理)。

This isn't possible. 这是不可能的。 The request made by the browser to populate the img tag needs to have a response with image content. 浏览器发出的填充img标签的请求需要包含图像内容的响应。 The HTML file is what comes back in that response and since that response doesn't contain a redirect header the browser will not make another request to anywhere. HTML文件就是该响应中返回的内容,并且由于该响应不包含重定向标头,因此浏览器不会向任何地方发出另一个请求。 Since the page is trying to load into an img tag not even JS on the page will help you because that JS isn't even going to run since the HTML document isn't being parsed by the browser as an HTML document. 由于该页面正试图加载到img标签中,因此页面上的JS都不会帮助您,因为该JS甚至都不会运行,因为浏览器未将HTML文档解析为HTML文档。 It will just result in a broken image link. 这只会导致图像链接损坏。

If you have no control over the server so you can control the responses to requests made to it then you are out of luck in this particular scenario. 如果您无法控制服务器,则可以控制对服务器的请求的响应,那么在这种特殊情况下,您将很不走运。


With that in mind, have you considered just using JavaScript on your page to conditionally load actual image URLs into the img tag based on whatever logic you need? 考虑到这一点,您是否考虑过仅使用页面上的JavaScript根据所需的逻辑将实际的图像URL有条件地加载到img标签中? What exactly is it that you're trying to do? 您到底想做什么? Here's a plain JavaScript example of toggling the source of an img tag on button click: 这是一个普通的JavaScript示例,它在单击按钮时切换img标签的来源:

http://codepen.io/anon/pen/PNzjxe http://codepen.io/anon/pen/PNzjxe

I need to be able to do this in pure HTML/JavaScript if at all possible (my servers don't allow any server-side processing). 我需要能够使用纯HTML / JavaScript做到这一点(我的服务器不允许任何服务器端处理)。

This cannot be done in pure JavaScript/HTML on the client-side . 这不能在客户端使用 JavaScript / HTML完成。

You need some form of server-side processing such as PHP or something else running on the server to do this. 您需要某种形式的服务器端处理(例如PHP)或服务器上运行的其他某种方式来执行此操作。

In general, i think what you are trying to do will be confusing later on when you review your code. 总的来说,我认为稍后您在查看代码时会尝试混淆。 For example, you (or another developer reviewing the code) may think it was a typo and then want to change .html to something like .jpg or .png 例如,您(或另一位检查代码的开发人员)可能认为这是拼写错误,然后想要将.html更改为.jpg.png

With that said, what you are trying to do could be accomplished using the following ways: 话虽如此,您可以通过以下方式完成您要尝试的操作:

Server-side processing (JavaScript) 服务器端处理(JavaScript)

A application can have custom HTTP Request Handlers . 应用程序可以具有自定义HTTP请求处理程序 This approach would be pure JavaScript however it requires server-side processing. 这种方法将是纯JavaScript,但是它需要服务器端处理。

Here is a JavaScript function that returns an image: 这是一个返回图像的JavaScript函数:

function doReturnImage(request, response) {
    var varImage = application.loadImage(application.getFolder().path+'WebFolder/'+'myPict.png');
    response.contentType = 'image/png';   
    return varImage;
}

Assuming the above function is defined in boot.js then here is the request handler calling the above function with addHttpRequestHandler : 假设在boot.js中定义了上述函数,那么这是使用addHttpRequestHandler调用上述函数的请求处理程序:

addHttpRequestHandler("/page2.html", "boot.js" , "doReturnImage");

Server-side processing (Alternative) 服务器端处理(替代)

A with the following code in the On Web Connection database method: On Web Connection数据库方法中具有以下代码的数据库:

// On Web Connection
C_TEXT($1;$2;$3;$4;$5;$6)
C_TEXT($url_t)
$url_t:=$1
Case of 
  : ($url_t="/page2.html")
    // return an image for this html request
    $folder:=Get 4D folder(Database folder)
    WEB SEND FILE($folder+"bird.png")
End case 

In the snippet of 4D code above, the request for /page2.html will result in the file bird.png being sent to the browser. 在上述4D代码段中,对/page2.html的请求将导致文件bird.png发送到浏览器。 This would allow you to use page2.html as the src of an img tag like this: 这将允许您将page2.html用作img标签的src ,如下所示:

<img src="page2.html">

This approach requires server-side processing. 这种方法需要服务器端处理。


A similar question was asked here 在这里提出类似的问题

I don't think so because of the src functionality. 由于src功能,我不这么认为。

The src attribute instructs the browser where on the server it should look for the image that's to be presented to the user. src attribute指示浏览器在服务器上应查找要呈现给用户的图像。 This may be an image in the same directory, an image somewhere else on the same server, or an image stored on another server. 这可能是同一目录中的映像,同一服务器上其他位置的映像或另一服务器上存储的映像。

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

相关问题 如何下载使用渲染的图像<img>标签? - How can I download image which is rendered with <img> tag? 我可以将图像从 img 标签上传到 firebase 存储吗 - Can I upload image to firebase storage from img tag 我无法使用我的Jquery:需要在点击时动态添加img标签 - I can't get my Jquery to work: Need to add img tag dynamically upon click 我无法验证标签img - I can't validate tag img 我该怎么做<img>点击时标签显示全尺寸图像,而该标签的href可以更改? - How can I make <img> tag show a full size image when clicked, while href of that tag can change? 我可以在JavaScript上使用img标签来显示存储在MongoDB中的图像吗? - Can I use the img tag on JavaScript to display my image stored in MongoDB? 如何获取div的背景图像值并将其应用于img标签onclick的src值? - How can I get the background-image value of a div and apply it to the src value of an img tag onclick? 我可以从画布元素获取图像并在 img src 标签中使用它吗? - Can I get image from canvas element and use it in img src tag? 如何在html img标签中使用Java字符串图像对象-有可能吗? - How can I use a java string image object in a html img tag - Is it possible? 如何修改此javascript,以便同时删除和 - How can I modify this javascript so it'll remove both <b> and <i> tags?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM