[英]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: 话虽如此,您可以通过以下方式完成您要尝试的操作:
A wakanda application can have custom HTTP Request Handlers . wakanda应用程序可以具有自定义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");
A 4d-database with the following code in the On Web Connection
database method: On Web Connection
数据库方法中具有以下代码的4d数据库:
// 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. 这种方法需要服务器端处理。
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.