jQuery Mobile and “query parameters” for hashbang navigation

后端 未结 3 1677
太阳男子
太阳男子 2021-02-20 03:13

I am using jQuery Mobile and have few pages in one HTML page. When opening these pages, I\'d like to pass parameters for them, so that their parameters are persistent in URL.

3条回答
  •  滥情空心
    2021-02-20 03:26

    I would suggest you don't use hash 'parameters', since current support for it is buggy.

    I would intercept the clicks on all links and look for a specific data- element, say, data-params:

        $('a').live('click',
            function(e) {
                var data = $(e.target).jqmData()
                globalParams = data.params !== null ? data.params : null
            }
        )
    

    And in your HTML you can go

    ....
    

    In this case you are creating a global variable, called params, which you should be able to access in a uniform manner from all your code.

    You will have to parse those parameters yourself though, however that's not hard, could use something like this:

    function getCurrentParams() {
    
        if (!params) {
            return null
        }
    
        var res = {}
        $(params.split('&')).each(
            function(i, e) {
                var pair = e.split('=')
                if (pair.length !== 2) {
                    return
                }
                res[pair[0]] = pair[1]
            }
        )
    
        return res
    }
    

提交回复
热议问题