【问题标题】:back button inside a iframe using history.js使用history.js的iframe内的后退按钮
【发布时间】:2013-09-23 04:18:24
【问题描述】:

我正在做一个 RoR 项目。该项目位于 iFrame 内。在那个 iframe 中有前进和后退按钮。当我单击后退按钮时,iframe 内容应该返回,而不是整个浏览器页面。我正在使用“histroy.js”插件。但我不知道如何使用它。

我的代码是这样的:

$('#back-btn').on('click', function () {
    History.back();
});

$('#forward-btn').on('click', function () {
    History.forward();
});

$("a").click(function () {
    n = 1;
    q = n++;
    var s = $(this).attr("href");
    var o = 'History.pushState({state:' + q + ',rand:Math.random()}, "State 1", \"' + s + '\");'
    $("#z").empty().append('<button onclick=\'javascript:' + o + '\'>sds</button>');
    $("#z button").click();


    //this is for checking log
    (function (window, undefined) {

        var
        History = window.History, // Note: We are using a capital H instead of a lower h
            State = History.getState(),
            $log = $('#log');

        // Log Initial State
        History.log('initial:', State.data, State.title, State.url);

        // Bind to State Change
        History.Adapter.bind(window, 'statechange', function () { // Note: We are using statechange instead of popstate
            // Log the State
            var State = History.getState(); // Note: We are using History.getState() instead of event.state
            History.log('statechange:', State.data, State.title, State.url);
        });
    })(window);
})

注意:有时我使用 ajax 调用。

感谢您的支持。

【问题讨论】:

    标签: javascript jquery ruby-on-rails iframe history.js


    【解决方案1】:

    您可能喜欢使用window.history 对象

    iframe.contentWindow.history.go(-1); // back
    iframe.contentWindow.history.go(1);  // forward
    

    所以你的后退按钮可以修改为\

    $('#back-btn').on('click', function () {
        iframe.contentWindow.history.go(-1);
                     or
        iframe.contentWindow.history.back(); 
    });
    

    你的前进按钮可以修改为

    $('#forward-btn').on('click', function () {
      iframe.contentWindow.history.forward();
                    or
      iframe.contentWindow.history.go(1);
    });
    

    干杯!

    【讨论】:

    • 这不是只有在 iframe 之外运行代码才有效吗?
    猜你喜欢
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多