簡體   English   中英

歷史HTML5和頁面源

[英]History HTML5 and page source

我正在使用html5歷史記錄API,但遇到以下問題:

  1. 轉到我的應用程序:app.mysite.io
  2. 使用歷史記錄API(app.mysite.io/hello等)瀏覽。 我不收費所有HTML頁面,而只收費我需要的部分內容。
  3. 關閉頁面(Chrome中為CTRL + W),然后重新打開頁面(Chrome中為CTRL + SHIFT + T)
  4. 此時,我僅看到最后一個AjaxCall及其響應(部分內容,因為我認為它看起來像是ajax請求)。

我創建了一個路由器,該路由器在URL更改時進行偵聽,但是無法檢測到何時關閉頁面然后重新打開。

編輯:我正在使用Symfony2,我加載了兩個不同的模板ajax-layout.html.twigfull-layout.html.twig

edit2那是我的代碼:

var Devmanager = {
    environment: 'prod',
    prefix: '',
    name: '',
    routes: [
        "dashboard/",
        "project/add/",
        "project/view\/(.*)\/",
        "project/",
        "team/add/",
        "team/view\/(.*)\/",
        "settings/upgrade/",
        "settings/",
        "help/support/",
        "help/video/",
        "help/suggest/"
    ],

    config: function(options){
        this.environment = options && options.environment;
        this.name = options && options.name;
        this.prefix = (this.environment === 'prod') ? '/' : '/app_dev.php/';

        Router.config({ mode: 'history', root: this.prefix});
    },


    register: function(){
        var that = this;

        for(var i=0; i < that.routes.length; i++){
            (function(){
                const _route = that.routes[i];
                Router.add(_route, function() {
                    arg0 = (arguments[0]) ? arguments[0] : '';
                    Devmanager.call({
                        path: Router.root + _route.replace('(.*)', arg0),
                        method: "GET",
                        data: {},
                        success: function(response, status, request){
                            $("section#content").html(response);
                        }
                    });
                });
            })();
        }
        Router.listen();
    },

    run: function(){
        this.register();
        this.bind();
    },

    bind: function(){
        $(document).on('click', '.nav-primary a, .navbar-nav  a.upgrade, .main-menu a:not(.external), .nav-project a', function (e) { Devmanager.goto(e) });
    },

    call: function(options){
        NProgress.start();
        $.ajax({
            url: options.path,
            data: options.data,
            method: options.method,
            success: options.success,
            error: options.error && function(){
                location.href = Router.root;
            },
            complete: function (request) {
                Devmanager.toolbar(request);
                NProgress.isStarted() && NProgress.done();
            }
        });
    },

    goto: function(e){
        var $this = $(e.target);
        $this.is('a') || ($this = $this.closest('a'));
        if(!$this.next().is('ul')) Router.navigate($this.attr('href'));
        e.preventDefault();
    },

    toolbar: function(request){
        if (typeof Sfjs === "undefined" || this.environment === 'prod') return;

        var sf = $('.sf-toolbar')[0];
        var xdebugToken = request.getResponseHeader('X-Debug-Token');
        Sfjs.load(sf.id, '/app_dev.php/_wdt/'+ xdebugToken);
    }
};

var Router = {
    routes: [],
    mode: null,
    root: '/',

    config: function(options) {
        this.mode = options && options.mode && options.mode == 'history'
        && !!(history.pushState) ? 'history' : 'hash';
        this.root = options && options.root ? options.root : '/';
        return this;
    },

    getFragment: function() {
        var fragment = '';
        if(this.mode === 'history') {
            fragment = this.clearSlashes(decodeURI(location.pathname + location.search));
            fragment = fragment.replace(/\?(.*)$/, '');
            fragment = this.root != '/' ? fragment.replace(this.root, '') : fragment;
        } else {
            var match = window.location.href.match(/#(.*)$/);
            fragment = match ? match[1] : '';
        }
        return this.clearSlashes(fragment);
    },

    clearSlashes: function(path) {
        return path.toString().replace(/\/$/, '').replace(/^\//, '');
    },

    add: function(re, handler) {
        if(typeof re == 'function') {
            handler = re;
            re = '';
        }
        this.routes.push({ re: this.clearSlashes(this.root + re), handler: handler});
        return this;
    },

    remove: function(param) {
        for(var i=0, r; i<this.routes.length, r = this.routes[i]; i++) {
            if(r.handler === param || r.re.toString() === param.toString()) {
                this.routes.splice(i, 1);
                return this;
            }
        }
        return this;
    },

    flush: function() {
        this.routes = [];
        this.mode = null;
        this.root = '/';
        return this;
    },

    check: function(f) {
        var fragment = f || this.getFragment();
        for(var i=0; i<this.routes.length; i++) {
            var match = fragment.match(this.routes[i].re);
            if(match) {
                match.shift();
                this.routes[i].handler.apply({}, match);
                return this;
            }
        }
        return this;
    },

    listen: function() {
        var self = this;
        var current = self.getFragment();
        var fn = function() {
            if(current !== self.getFragment()) {
                current = self.getFragment();
                self.check(current);
            }
        };
        clearInterval(this.interval);
        this.interval = setInterval(fn, 50);
        return this;
    },

    clearRoot: function (path) {
        return path.replace(this.root, '');
    },

    navigate: function(path) {
        path = (path == undefined) ? '' : this.clearRoot(path);
        if(this.mode === 'history') {
            History.pushState(null, Devmanager.name, this.root + this.clearSlashes(path) + '/');
        } else {
            window.location.href.match(/#(.*)$/);
            window.location.href = window.location.href.replace(/#(.*)$/, '') + '#' + path;
        }
        return this;
    }
};

我已解決問題,並且正在為遇到相同問題的用戶寫信:

在$ .ajax方法中插入選項cache:false ,如下所示:

$.ajax({
     url: options.path,
     data: options.data,
     method: options.method,
     success: options.success,
     cache: false,
     error: options.error && function(){
         location.href = Router.root;
     },
     complete: function (request) {
         Devmanager.toolbar(request);
         NProgress.isStarted() && NProgress.done();
     }
 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM