Skip to content Skip to sidebar Skip to footer

How To Open The Newly Created Image In A New Tab?

Below code creates the image in the bottom of the same page. How to display that image into a new tab/window instead of displaying in the same page? success: function (data) {

Solution 1:

something like:

success: function (data) {
        var image = newImage();
        image.src = "data:image/jpg;base64," + data.d;

        var w = window.open("");
        w.document.write(image.outerHTML);
    }

Solution 2:

Current suggestions were not working on chrome, I was always getting a white page, now I am using

const base64ImageData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
const contentType = 'image/png';

const byteCharacters = atob(base64ImageData.substr(`data:${contentType};base64,`.length));
const byteArrays = [];

for (let offset = 0; offset < byteCharacters.length; offset += 1024) {
    const slice = byteCharacters.slice(offset, offset + 1024);

    const byteNumbers = newArray(slice.length);
    for (let i = 0; i < slice.length; i++) {
        byteNumbers[i] = slice.charCodeAt(i);
    }

    const byteArray = newUint8Array(byteNumbers);

    byteArrays.push(byteArray);
}
const blob = newBlob(byteArrays, {type: contentType});
const blobUrl = URL.createObjectURL(blob);

window.open(blobUrl, '_blank');

Thanks to Jeremy! https://stackoverflow.com/a/16245768/8270748

Solution 3:

Latest solution - works 2019-10.

Open image in new tab.

let data = 'data:image/png;base64,iVBORw0KGgoAAAANS';
let w = window.open('about:blank');
let image = newImage();
image.src = data;
setTimeout(function(){
  w.document.write(image.outerHTML);
}, 0);

https://stackoverflow.com/a/58615423/2450431https://stackoverflow.com/a/46510790/2450431https://stackoverflow.com/a/27798235/2450431

Solution 4:

demo

Updated DEMO (working in Chrome, even with pops blocked) - 3/3/2021

wrapping the call to window.open circumvents the issues detailed below.

document.getElementById('my_button').addEventListener('click', (evt) => {
  window.open("https://via.placeholder.com/150", '_blank')
});

Last tested in Chrome 88 on 64 bit Windows 10.

Post a Comment for "How To Open The Newly Created Image In A New Tab?"