哈希在页面加载时打开模态

时间:2015-09-26 17:40:58

标签: jquery twitter-bootstrap hash modal-dialog

我有以下代码片段,应该检查哈希值,以及是否有哈希打开引导程序模式。我的模态标记是用把手动态生成的。在hashchange事件上工作正常,但在页面加载时,哈希模式不会打开。

    //Render Handlebars
    var iniCycles = function() {

            var r = $.Deferred();
            var data = [];
            if ($('ul.cycle-slideshow').length) {
                $('ul.cycle-slideshow').map(function() {
                    var hash = $(this).data('id');
                    var source = $("#" + hash).html();
                    data.push( {'image': $(this).find('img').prop('src') });
                    var template = Handlebars.compile(source);
                    $('body').append(template(data));
                }).get();
            }
            return r;
    };

    var modalOpen = function (hashtag) {
         $(hashtag).modal('toggle');
         $(hashtag).on('hidden.bs.modal', function () {
         history.pushState("", document.title, window.location.pathname);
            });
    };


$(document).ready(function () {

    //This part is working
    $(window).on('hashchange', function() {
        var modal_hash = window.location.hash; 
        modalOpen(modal_hash);
    });

    //on page load check for the hash, if there is a hash call modalOpen. This one in function is empty is something to do with handlebars I think
    if(window.location.hash) {
        var modal_url = window.location.hash; 
        iniCycles().done( modalOpen(modal_url) );

    }
});

0 个答案:

没有答案