I would like to display the image from a network camera on my web page, but the image is behind a HTTP basic authentication server.
In Firefox and Chrome I can do this:
<img width="320" height="200" src="http://username:password@server/Path" />
But in Internet Explorer 8, I get an empty image box. If I use JQuery to set the src attribute, IE8 displays a blank src. It looks like IE8 is checking the string and rejecting it.
Is there a way to put the basic authentication credentials in the img tag?
The img element has two required attributes: src : The source location (URL) of the image file. alt : The alternate text. This is used to describe the image for someone who cannot see it because they are either using a screen reader or the image src is missing.
Basic Auth creds are "plaintext over SSL" just like sending your credit card number through HTTP POST is "plaintext over SSL". So by doing it over SSL (TLS these days) makes it secure during transport.
Bottom line: Not all browsers allow this. It may work in some but not others.
But as someone else has said already, it's not very safe -- you're effectively giving the login and password details to anyone who browses the page. Not good.
A better option would be proxy it through the same server that you're providing the html code from, then the href in the <img>
tag could just be a local URL, and no-one need know where the image is actually coming from.
You can load your img
with AJAX, using XMLHttpRequest. As you might know, XMLHttpRequest has a setRequestHeaders
method, so you will be able to manipulate headers for your request, hence, you will be able to do basic HTTP authentication.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With