'How do I display image in Alert/confirm box in Javascript?
How to display image in alert box or confirm box? I have been trying with below code but getting image url in the alert box. Please anybody help me to get solve or please give any other suggestions if it does not possible.
var image = document.getElementById("myImage").src="hackanm.gif";
alert("OnLoad image"+image );
Solution 1:[1]
Snarky yet potentially useful answer:
http://picascii.com/ (currently down)
https://www.ascii-art-generator.org/es.html (don't forget to put a \n after each line!)
Solution 2:[2]
Short answer: You can't.
Long answer: You could use a modal to display a popup with the image you need.
You can refer to this as an example to a modal.
Solution 3:[3]
As other have mentioned you can't display an image in an alert. The solution is to show it on the webpage.
If I have my webpage paused in the debugger and I already have an image loaded, I can display it. There is no need to use jQuery; with this native 14 lines of Javascript it will work from code or the debugger command line:
function show(img){
var _=document.getElementById('_');
if(!_){_=document.createElement('canvas');document.body.appendChild(_);}
_.id='_';
_.style.top=0;
_.style.left=0;
_.width=img.width;
_.height=img.height;
_.style.zIndex=9999;
_.style.position='absolute';
_.getContext('2d').drawImage(img,0,0);
}
Usage:
show( myimage );
Solution 4:[4]
Use jQuery dialog to show image, try this code
<html>
<head>
</head>
<body>
<div id="divid">
<img>
</div>
<body>
</html>
<script>
$(document).ready(function(){
$("btn").click(function(){
$("divid").dialog();
});
});
</script>
`
first you have to include jQuery UI at your Page.
Solution 5:[5]
You can emojis!
$('#test').on('click', () => {
alert('? Build is too fast');
})
Solution 6:[6]
I created a function that might help. All it does is imitate the alert but put an image instead of text.
function alertImage(imgsrc) {
$('.d').css({
'position': 'absolute',
'top': '0',
'left': '50%',
'-webkit-transform': 'translate(-50%, 0)'
});
$('.d').animate({
opacity: 0
}, 0)
$('.d').animate({
opacity: 1,
top: "10px"
}, 250)
$('.d').append('An embedded page on this page says')
$('.d').append('<br><img src="' + imgsrc + '">')
$('.b').css({
'position':'absolute',
'-webkit-transform': 'translate(-100%, -100%)',
'top':'100%',
'left':'100%',
'display':'inline',
'background-color':'#598cbd',
'border-radius':'4px',
'color':'white',
'border':'none',
'width':'66',
'height':'33'
})
}
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
<div class="d"><button onclick="$('.d').html('')" class="b">OK</button></div>
.d{
font-size: 17px;
font-family: sans-serif;
}
.b{
display: none;
}
Solution 7:[7]
Since JS dialogs display only text, you could try using Emojis. For example:
alert('Hello friend! \u{1F642}');
You just need to use the unicode hex value for the emoji you want. Here is a list of available emojis: https://unicode.org/emoji/charts/emoji-list.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sergio A. |
| Solution 2 | Community |
| Solution 3 | Michaelangel007 |
| Solution 4 | Sergio A. |
| Solution 5 | SimonRabbit |
| Solution 6 | Daniel |
| Solution 7 | Mark Torres |
