I\'m trying to set an \'src\' by calling a javascript function that returns the path like so:
the fun
Building on Eugen's answer, I wanted something self-contained (no id's, as inline as possible) that would not require a hosted pixel.gif
image. I came up with a few possibilities:
One option would be to use a base64 encoded src URL instead (as small as possible). Note that data-uris are supported in IE8+:
See it in action on jsfiddle.
A second option would be to use document.write
to write the image tag directly with an inline script. Just put the below instead of the
. Note that many consider document.write
to be bad practice:
See it in action on codepen.io.
A third (perhaps even more hackish) option would be to forcibly break the image by supplying a null src, and (ab)use the onerror
callback.
This works in IE11 and Chrome, but not Firefox:
See it in action on jsfiddle.
This fourth option relies on a simple function that sets an
tag followed immediately by an inline that sets the image's src via JavaScript:
And in your :
See it in action on codepen.io.
Summary: I'm just ideating. Each option has different implications and requirements -- they're all workarounds, and should be tested thoroughly for your specific use case. Personally I think the first option (base64 URI) is the most solid (if you ignore old IE browsers, which I happily can).