Track only iframe history

后端 未结 6 710
囚心锁ツ
囚心锁ツ 2021-01-13 06:06

I have a page which contains an iframe and I want to track the history of the iframe only. I tried to use the history object like this:



        
6条回答
  •  盖世英雄少女心
    2021-01-13 06:59

    If the frame contains contentWindow, you can use frame.contentWindow.history.

    But not all browsers support this property. If not, the frame's location should be stored in some variables. Bind event handler on your frame onload event like below(using jquery)

    var frameHistory = [];
    $(document).ready(function(){
        var frame = window.frames["test2"];
        $.bind(frame, "onload", function(){ //store location when frame loads
            frameHistory.push(frame.window.location.href);
        }
    });
    

    And in your button click event handler:

    alert(frameHistory.length);
    

提交回复
热议问题