首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >历史HTML5和页面源

历史HTML5和页面源
EN

Stack Overflow用户
提问于 2014-11-06 16:28:18
回答 1查看 79关注 0票数 1

我使用的是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,这是我的代码:

代码语言:javascript
复制
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;
    }
};
EN

回答 1

Stack Overflow用户

发布于 2014-11-06 21:51:08

我解决了这个问题,我给有同样问题的用户写了一篇文章:

cache:false方法中插入选项$.ajax,如下所示:

代码语言:javascript
复制
$.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();
     }
 });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26784438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档