简体   繁体   English

使用后退按钮时IE中的JavaScript问题

[英]JavaScript issue in IE when using the back button

I have a form where users can query about two different regions. 我有一个表单,用户可以在其中查询两个不同的区域。 Form elements for region one are shown by default when the page first loads, while form elements for region two are hidden. 默认情况下,页面第一次加载时显示区域1的表单元素,而区域2的表单元素则隐藏。 I have a select box where users can choose which region they wish to query about, and a bit of JavaScript to show and hide different portions of the form depending on what the region is. 我有一个选择框,用户可以在其中选择要查询的区域,还可以使用一些JavaScript来显示和隐藏表单的不同部分,具体取决于该区域是什么。

This works fine in Firefox and IE except when the user presses the browser back button in IE, which happens often when users wish to make a slightly different query. 除了在用户按下IE中的浏览器后退按钮(通常在用户希望进行稍有不同的查询时)时,这种方法在Firefox和IE中可以正常工作。 If the user queried about region two, then pressed the back button in IE, the region selector still says "Region Two" but form elements for region one are shown. 如果用户查询了区域2,然后在IE中按下了返回按钮,则区域选择器仍显示“区域2”,但显示了区域1的表单元素。 I expect form elements for region two to still show like Firefox does, but how? 我希望区域2的表单元素仍然像Firefox一样显示,但是如何?

The following is a brief example of my code: 以下是我的代码的简短示例:

<script language="javascript" type="text/javascript">
<!-- 

 // Switch between regions
 function changeRegionType(form){
  // get the various elements we whish to manipulate
  var reg1 = getElementById('region_1');
  var reg2 = getElementById('region_2');

  // hide one row, show the other
  if (form['region_type'].value == 'region_1') {
   reg1.style.display = '';
   reg2.style.display = 'none';
  } 
  else if (form['region_type'].value == 'region_2') {
   reg1.style.display = 'none';
   reg2.style.display = '';
  }

  }

// --> 
</script>


<form name="QueryForm" action="something.php" method="get">

 <select name="region_type" onChange="changeRegionType(this.form)">
  <option selected value="region_1">Region One</option>
  <option value="region_2">Region Two</option>
 </select>

 <tr id="region_1">
  <!-- Show this row to people who want to query about region 1 -->
 </tr>

 <tr id="region_2" style="display: none;">
  <!-- Show this row to people who want to query about region 2 -->
 </tr>

</form>

Form fields remember their values over page back/forwards commands, so when you come back to the previous page, the last selected value 'region two' is reselected in the dropdown, but you won't get an onchange to tell you it has happened. 表单字段会通过上一页后退/前进命令记住它们的值,因此,当您返回上一页时,将在下拉列表中重新选择最后一个选定的值“区域二”,但是您将无法通过onchange得知发生了更改。

Consequently you should call the changeRegionType function to update the DOM from the selection, on the load event. 因此,您应该在load事件中调用changeRegionType函数以从选择中更新DOM。 ISTR some browser (Opera?) actually does the form update just after onload , so it may pay to fire the function on a 0-timeout in the onload instead. ISTR一些浏览器(Opera?)实际上是在onload之后才进行表单更新的,因此它可能需要在onload的0超时时触发该函数。

You're not seeing the same in Firefox due to the bfcache. 由于bfcache,您在Firefox中看不到相同的内容。 This keeps the previous page when you leave it, just not displayed. 当您离开它时,这将保留前一页,只是不会显示。 When you go back to it, it can un-hide the existing page DOM as if it had never been left. 当您回到它时,它可以取消隐藏现有页面DOM,就好像它从未被遗忘一样。 (This is done to increase the speed of back/forward navigations.) When the bfcache is disabled or your page falls out of it due to other pages crowding it out, you'll see the same behaviour as in IE and the other browsers. (这样做是为了提高后退/前进导航的速度。)如果禁用bfcache或由于其他页面将其挤掉而导致页面掉出页面,您将看到与IE和其他浏览器相同的行为。 (Safari 4 now also has a similar bfcache feature.) (Safari 4现在也具有类似的bfcache功能。)

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

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