gpt4 book ai didi

JavaScript 仅在页面重新加载时起作用

转载 作者:行者123 更新时间:2023-11-28 02:58:16 25 4
gpt4 key购买 nike

我有一个相当简单的页面,在弹出窗口中显示图像。 onLoad 事件只需获取查询字符串,用该值填充图像标记,然后调整图像和窗口的大小以匹配。

出于某种原因,这仅在页面重新加载/刷新时对我有效。或者,一旦图像显示一次,即使缓存已关闭,它也会从那时起起作用。但是,每当第一次查看图像时,页面都会显示为空白,并且不会引发任何错误。

有什么建议吗?

谢谢

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Image Zoom</title>
<meta http-equiv="Pragma" content="no-cache" />
<script type="text/javascript">
function getImage() {
//get the query string (everything after the ?)
var filename = window.location.search.substring(1);
//find the image control
var imageWorking = document.getElementById('dynamicImage');
//assign the image source
imageWorking.src = '../Images/' + filename;
//create an image variable
var newImg = new Image();
//assign the source to match the page image
newImg.src = imageWorking.src;
//get the width and height
var width = newImg.width;
var height = newImg.height;
//set the page image width and height to match the disk image
imageWorking.width = width;
imageWorking.height = height;
//set the window to be just larger than the image
window.resizeTo(width + 50,height + 50);
}
</script>
</head>
<body onload="getImage();">
<img src="images/spacer.gif" id="dynamicImage" alt="Image Zoom" width="1" height="1" />
</body>
</html>

使用以下函数调用该页面:

function imageZoom(imageName)
{
window.open('ImageZoom.htm?' + imageName + '','imageZoom','width=400,height=400,menubar=no,toobar=no,scrollbars=yes,resizable=yes');
}

最佳答案

在页面加载期间使用 JavaScript 动态编写 IMG 标记,并在加载时调整窗口大小:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Image Zoom</title>
<meta http-equiv="Pragma" content="no-cache" />
<script type="text/javascript">
function sizeWindow() {

//find the image control
var img = document.getElementById('dynamicImage');

//set the window to be just larger than the image
window.resizeTo(img.width + 50, img.height + 50);
}
</script>
</head>
<body onload="sizeWindow();">
<script type="text/javascript">
var filename = window.location.search.substring(1);
document.write("<img src='../Images/" + filename + "' id='dynamicImage' alt='Image Zoom' />");
</script>
</body>
</html>

关于JavaScript 仅在页面重新加载时起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2022023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com