I can't make this open the image in a new window (not tab).
This script is placed in <head>
:
<script type="text/javascript">
function randomImg1() {
var myImage1 = new Array();
myImage1[0] = "1.jpg";
myImage1[1] = "2.jpg";
myImage1[2] = "3.jpg";
var random = Math.floor(Math.random() * myImage1.length);
document.getElementById("image").innerHTML = "<img src='"
+ myImage1[random] + "' alt='image'></img>";
}
</script>
My button is placed in <body>
:
<button onclick="randomImg1();OpenInNewTab();">click</button>
<div id="image"></div>
Use https://www.w3schools.com/jsref/met_win_open.asp to open a new window. Add the spec
parameter to make it open as a new window instead of a tab. And make sure to pass the full path of the image:
<script type="text/javascript">
function randomImg1() {
var myImage1 = new Array();
myImage1[0] = "1.jpg";
myImage1[1] = "2.jpg";
myImage1[2] = "3.jpg";
var random = Math.floor(Math.random() * myImage1.length);
// create the full image URL
var imageUrl = window.location.protocol + '//' + window.location.host + window.location.pathname + myImage1[random];
// open in a new window. ideally use the image's size for w and h
window.open(imageUrl, '_blank', 'height=300,width=300');
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.