Microsoft Edge Has A BUG! – JS Returns WRONG value on Back Arrow

After clicking the back arrow in Microsoft Edge, my JavaScript runs once again. But there’s a BIG problem!

The values returned by “getElementById(“options”).value” are NOT the correct values for the option that is visible from the dropdown selection.

Firefox and Safari don’t have this problem, so why is Microsoft Edge and Chrome still Buggy after all these years? And therefore how can I overcome this mismatch between the returned getElementById().value and the visibly selected option?

Why does this matter? Because I’m sending dropdown selected values to the server-side via AJAX for an ecommerce shopping cart, and so the returned value needs to match the visible selection for the given dropdown before being sent over.

Naturally customers will occasionally click on the back arrow, and when they do, various values don’t always match with the visible selection – the exact mismatch depends on the particular browser.

So, after detecting that the page has been landed on by the user having clicked the back arrow, I then simply call getElementById().value for all of the dropdowns on the page, pass the values to the server via AJAX, and then use the newly calculated values that are returned by AJAX to refresh the page – a solution that works perfectly for Firefox and Safari, but NOT for Edge and Chrome.

Therefore, I’ve created a small test program that demonstrates the problem perfectly. You’ll notice that I’ve included the code (two HTML files), and a 33 second video which shows the test program running, with Edge on the left and Firefox on the right.

As shown in the video, the steps to reproduce the issue are as follows…

  1. Select option 2
  2. Navigate to page 2 by clicking the link
  3. Click the back arrow

Here’s the video: https://youtu.be/J3tLFXjPDdA

Can you see the problem? within window.onload, getElementById(“options”).value returns “option-1” after clicking back arrow, which does NOT match the selected option “Option 2” that is visible.

Bizarrely, I then discovered that just by delaying the call to getElementById() by setting setTimeout(), the value returned then becomes “option-2” which IS Correct, even for a delay value of 0.

I don’t want to simply reload the page on back arrow as a fix. I’d much rather learn why this mismatch of visible selection vs returned value is happening? If you think this is technically NOT a BUG, then please, I’d love to hear your explanation!

To run the test program, for each of the two pages simply copy the code into a text file and change the extension to .htm, and then right click on …page-1.htm and select open with Edge.

<!DOCTYPE html>

<label for="options">Select Option</label>
<select id="options">
  <option value="option-1">Option 1</option>
  <option value="option-2">Option 2</option>
</select>

<a href="back-arrow-issue-page-2.htm">Page 2</a>

<script>
  window.onpageshow = function(event) // testing for bfcache
  {
    if (event.persisted)
      alert("From bfcache");
  };

  window.onunload = function(){}; // disables bfcache in Firefox when running an htm file from a local drive, so that the script will run for a second time, i.e. after clicking back arrow. Note: this doesn't disable bfcache when running a local htm file in Safari

  window.onload = function()
  {
    console.log("in: window.onload");
    let performance_entries = performance.getEntriesByType("navigation");

    for (var i = 0; i < performance_entries.length; ++i)
    {
      console.log(performance_entries[i].type);

      if (performance_entries[i].type == "back_forward")
      {
        console.log(document.getElementById("options").value);
        setTimeout(delay_test, 0); // 1 worked, and so I then tried 0 which also works
      }
    }
    function delay_test()
    {
      console.log(document.getElementById("options").value);
    }
  }
</script>
<!DOCTYPE html>

<a href="back-arrow-issue-page-1.htm">Page 1</a>

Cheers, Gary.

New contributor

Gary Townsend GaryDT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật