简体   繁体   English

浏览器返回按钮后保留 JavaScript 功能

[英]Keep JavaScript function after browser go back button

I'm using JavaScript getElementById function to show a picture on my page when the drop down value is selected.我正在使用JavaScript getElementById函数,在选择下拉值时在我的页面上显示图片。 But after submit if I use the browser back button the picture is gone.但是提交后,如果我使用浏览器后退按钮,图片就消失了。 I realize this is probably a Cache-Control issue.我意识到这可能是缓存控制问题。 Is there anyway I can keep this function other than using Cache-control?无论如何,除了使用缓存控制之外,我还能保留这个功能吗? I am committing lots of variables to session cookies and I'm afraid to mess around with cache-control.我向会话 cookie 提交了很多变量,我害怕与缓存控制混为一谈。

function check_dd() {
    if(document.getElementById('show_pic').value == "") {
        document.getElementById('test').style.display = 'none';
    } else {
        document.getElementById('test').style.display = 'block';
    }
}

<select class="default" id="show_pic" onchange="check_dd();">
    <option value="" selected>Select question...</option>
    <option value="1">Question One</option>
    <option value="2">Question Two</option>
    <option value="3">Question Three</option>
    <option value="4">Question Four</option>
    <option value="5">Question Five</option>
    <option value="6">Question Six</option> 
</select>

<div id="test" style="display:none;"><img src="images/dcard.jpg" width="60" height="120" alt=""/></div>

How can I display the image after browser back button?如何在浏览器后退按钮后显示图像?

My function was tied to the value of a select option which was already getting stored in the browser cache.我的函数与已经存储在浏览器缓存中的选择选项的值相关联。 So I found an easy way to fix this issue by just using:所以我找到了一种简单的方法来解决这个问题,只需使用:

<body onload="check_dd()">

This way I didn't have to add any extra code.这样我就不必添加任何额外的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM