インラインフレーム(iframe)の参照(src)を書き換えるとブラウザの履歴に残る
iframe.src = url;
同じページ内のiframeにサムネを表示するスクリプトを作ったときに発覚
(iframe表示しているページが履歴に何個も残っていた)
解決方法
iframeのlocationをreplaceで変更する
【(location.replace(replace_path)でページ移動すると実行したページが履歴に残らない】
$('#N2GNV_list a').hover(function () { var iframe = document.getElementById('target'); var src = 'http://target.co.jp/' + tail; //履歴残さない iframe.contentDocument.location.replace(src); } samne.style.display = 'block'; }, function () { samne.style.display = 'none'; } );
だが、しかし!
これだとhover一回目は正常に処理されるのに、hover二回目にエラーが発生してしまった
エラー:「iframe.contentDocument が null」
…なんでだ?
(ちなみにこのとき、iframe 自体は null じゃありませんでした)
どういうこと?
解決方法
$('#N2GNV_list a').hover(function () { var iframe = document.getElementById('target'); <~省略~> if (iframe.contentDocument == null) { var iframed = (iframe.contentWindow || iframe.contentDocument); iframed.location.replace(src); } else { iframe.contentDocument.location.replace(src); } samne.style.display = 'block'; }, function () { samne.style.display = 'none'; } );
※if文はデバッグの名残
var iframed = (iframe.contentWindow || iframe.contentDocument);
iframed.location.replace(src);
こうすれば問題なく動いたがよくわからない
追記
本来はクロスブラウザ用の対策なんだと思う…
関係ありそうなページ?
local MDC: chrome コードでウィンドウを取り扱う (/ja/working_with_windows_in_chrome_code)
そのドキュメントには browser.contentDocument を、そのドキュメントの Window オブジェクトには browser.contentWindow を使ってアクセスできます。
二回目のresizeではドキュメントを参照できず、ウィンドウオブジェクトを参照しないといけないのか?
※環境:firefox 32.0 greasemonkey2.2