jQuery 处理 URL

URL可以用做WebApp的标记,当页面刷新时,再分析URL来显示对应的模块,载入相应的内容。

// window.location.href
// @2016.12.31
(function($) {
    $.Url = {
        PageWithParams: function() {
            var url = window.location.href;
            var page = url.split('/');
            return page[page.length-1];
        },
        // index.html?x=a or ?x=a
        // index.html#token or #token
        PageOnly: function() {
            var url = $.Url.PageWithParams();
            var page = url.split('#');
            if (page.length < 1) {
                page = url.split('?');
            }
            return page[0];
        },
        // get value by key from url
        Get: function(key) {
            var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        },
        // redirect
        Goto: function(url) {
            $(window.location).attr('href', url);
        }
    },
    $.Tab = {
        // "F5" refresh
        Reload: function() {
            window.location.reload();
        },
        // "^W"
        Close: function() {
            window.opener = null; window.open(".", "_self"); window.close();
            if (window) { window.location.href = "about: blank"; }
        }
    };
}) (jQuery);

// HOWTO call
$(function() {
    var url = $.Url.PageWithParams();
    console.log('Current page:', url);

    var token = $.Url.Get('token');
    if (token)
    {
        $.Url.Goto('dashboard.html?token='+token);
    }
});

results matching ""

    No results matching ""