Skip to content

Instantly share code, notes, and snippets.

@kuboon
Last active September 10, 2015 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuboon/9524b176644cc5512207 to your computer and use it in GitHub Desktop.
Save kuboon/9524b176644cc5512207 to your computer and use it in GitHub Desktop.
Avoid the behavior that Edge browser does not persist checkbox and radio state on history.back()
<input type="hidden" id="edge_back_data_store" />
<script type="text/javascript">
+function(){
var edge_ver = function(){
var match = navigator.userAgent.match(new RegExp('Edge/(.+)'));
return match ? parseFloat(match[1]) : 999
}
if(12.1024 < edge_ver()) return;
var data_store = document.getElementById('edge_back_data_store');
if(!data_store) return;
var data_hash,
document_onload = function(){
var targets = document.querySelectorAll('input[type=radio], input[type=checkbox]');
data_hash = data_store.value ? JSON.parse(data_store.value) : {};
Array.prototype.forEach.call(targets, function(node) {
var val = data_hash[node.name];
if(val){
node.checked = (node.type == 'radio' ? (node.value == val) : ('true' == val));
}
node.addEventListener("click", elem_onclick);
});
},
elem_onclick = function(){
data_hash[this.name] = (this.type == 'radio' ? this.value : this.checked)
data_store.value = JSON.stringify(data_hash);
};
document.addEventListener('DOMContentLoaded', document_onload);
}();
</script>

history.back() 時の Edge ブラウザの特徴的挙動

  • checkbox と radio の checked 属性が保持されない
    • 保持されないどころか、 [input type='radio' checked] が back() で戻ってくると選択されてない

history.back() 時のブラウザ一般の挙動 (たぶん)

  • dataset ( jQueryでいうところの $elem.data('hoge') でアクセスできるやつ ) は一切保持されない。
  • Javascript で hidden の input 要素を生成した場合、要素自体保持されない。
  • あらかじめ hidden 要素が html に存在しており、 そこに Javascript で value を指定したものは、 DOMContentLoaded 後にその値を読めるようになる。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment