刷新后数据持久化
我有一个网页遇到问题。它工作正常:您从下拉菜单中进行选择,图像就会更新。
问题是当手动刷新页面时:显示初始图像,但下拉菜单选项保持不变;好吧,我解决了这个问题。
真正的问题是,刷新后,当我选择一个新项目时,由于某种原因会显示刷新之前的项目。
如何更改此设置,以便显示刷新后的新选择而不是上一个选择?
我的代码在 Chrome、Firefox 和 Safari 中运行良好,但在 IE9 中运行不佳。
这是我正在使用的 JavaScript 代码:
function doMillviewInitialise()
{
window.document.forms[0].reset();
}
这是 HTML:
<form>
<select id="millviewStyle" onchange="doMillview();">
<option value="self">Millview @ Guygar.com©</option>
<option>----------------------</option>
<option value="midnightExpress">Midnight Express</option>
<option value="turnedOn">Turned On</option>
<option value="storage">Storage</option>
<option value="plasticStress">Plastic Stress</option>
<option value="mirrorEffect">Mirror Effect</option>
<option value="okComputer">OK Computer</option>
<option value="repertoire">Repertoire</option>
<option value="calibrate">Calibrate</option>
<option value="hysteria">Hysteria</option>
<option value="lastExit">Last Exit</option>
</select>
</form>
I have a web page that I am having trouble with. It works fine: you select from the drop-down menu and the image updates.
The issue is when the page is manually refreshed: The initial image is displayed but the drop-down menu option stays the same; well, I fixed that.
The real issue is that after the refresh, when I choose a new item, the item before the refresh is displayed for some reason.
How can I change this so the new selection after the refresh is displayed and not the previous one?
My code works fine in Chrome, Firefox, and Safari, but not IE9.
This is the JavaScript code I am using:
function doMillviewInitialise()
{
window.document.forms[0].reset();
}
And here is the HTML:
<form>
<select id="millviewStyle" onchange="doMillview();">
<option value="self">Millview @ Guygar.com©</option>
<option>----------------------</option>
<option value="midnightExpress">Midnight Express</option>
<option value="turnedOn">Turned On</option>
<option value="storage">Storage</option>
<option value="plasticStress">Plastic Stress</option>
<option value="mirrorEffect">Mirror Effect</option>
<option value="okComputer">OK Computer</option>
<option value="repertoire">Repertoire</option>
<option value="calibrate">Calibrate</option>
<option value="hysteria">Hysteria</option>
<option value="lastExit">Last Exit</option>
</select>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
iGuygar IE9 存在 iframe 以及其他相当标准的浏览器功能(例如 CSS3 文本阴影)的问题,您可以做的是创建一个 div,然后将其放置在距离屏幕大约 10000 像素的位置,然后在 div 中预加载图像。
因此,您的 div 的 HTML:
CSS:
有些人还喜欢添加(不必要的,移动客户除外):
到该 CSS 中。这将在所有已知的浏览器中预加载您的图像。
iGuygar IE9 has issues with iframes as well as other pretty standard browsers features (the CSS3 text-shadow for example) what you can do is create a div and then position it absolutely about 10000 pixels off the screen then preload your images in the div.
So your div's HTML:
The CSS:
some like to also add (unnecessarily, except for mobile customers):
to that CSS. This will preload your images in all known browsers.
如果您需要的话,有几种方法可以将图像加载(或预加载)到页面中。您可以使用 javascript 将所有图像加载到“隐藏”容器中 (
)。
浏览器会缓存这些图像,因此如果您愿意,您甚至可以在加载所有图像后删除容器!
另一个优点是,在页面刷新时不会从服务器重新加载图像。如果您希望在刷新后加载页面时显示特定图像,我建议将
window.location.hash
(URL 哈希)更改为图像的 ID,并在以下情况下读取此属性:页面加载并显示适当的图像。http://..../millview/page.html#midnightExpress
然后:
尝试一下...您可能会发现使用 URL 哈希作为一种“持久性”非常有用页面/刷新/历史记录也是如此。
(如果您认为本文太冗长且无用,请回帖!)
浏览器(包括 IE)通过 URL 缓存图像。因此,只要预加载和当前正在查看的图像的 src 属性保持不变,图像就不会从服务器重新加载,而只是从缓存中获取 - 这非常快。一旦图像加载,预加载的图像元素就可以被丢弃 - 我们只希望浏览器缓存图像
There are a few ways to load (or preload) images into a page, if that's what you're after. You can use javascript to load all images into a 'hidden' container (
<div style="display: none">
).Browsers would cache these images, so you could even get rid of the container after all images load, if you want!
An added advantage is that images won't be reloaded from the server in the case of a page refresh. If you want a particular image to be shown when the page loads after a refresh, I would suggest changing the
window.location.hash
(URL hash) to the ID of the image, and read this property when the page loads and display the appropriate image.http://..../millview/page.html#midnightExpress
and then:
Experiment with this... you might find it really useful to use URL hashes as a sort of "persistence" across pages/refreshes/history, too.
(Post back if you think this was too long winded and useless!)
Browsers, including IE, cache images by their URL. So, as long as the
src
attribute remains the same for the preloaded and currently viewing image, the image won't be reloaded from the server, just picked up from the cache - which is quite fast. The preloaded image element can be discarded once the image has loaded - we only want the image cached by the browser页面加载后,您可以强制重置表单(您也需要一个表单):
After your page loads, you can force a reset on the form (you'll need a form too):
我看到你所描述的问题,我的猜测是 IE 正在缓存 iframe。事实上,当我用IE9的开发者工具查看iframe时,我可以看到它正在加载iframe中先前选择的文档。
我建议简化你的代码。我不知道您的最终目标是什么,但我为您创建了一个不使用 iframe 的简单测试用例。您可以在 这个 JSFiddle 中实时查看它。请注意,我使用 jQuery 来简化 DOM 操作。
HTML:
JavaScript:
I see the issue you are describing, and my guess is that IE is caching the iframe. In fact, when I look at the iframe with IE9's developer tools, I can see that it is loading the previously selected document in the iframe.
I suggest simplifying your code. I don't know what your end goal is, but I have created a simple test case for you that does not use an iframe. You can view it live in this JSFiddle. Note that I have used jQuery to simplify the DOM manipulation.
HTML:
JavaScript: