Getting current page url in jquery mobile

17,999

Solution 1

Isn't this you are looking for:

var currentUrl = $.mobile.activePage.data('url');

Solution 2

jQuery Mobile has a URL parsing method called $.mobile.path.parseUrl. It's use would be something like this:

var obj = $.mobile.path.parseUrl("http://jblas:[email protected]:8080/mail/inbox?msg=1234");

Which would return the following object:

//  obj.href:         http://jblas:[email protected]:8080/mail/inbox?msg=1234&type=unread#msg-content
//  obj.hrefNoHash:   http://jblas:[email protected]:8080/mail/inbox?msg=1234&type=unread
//  obj.hrefNoSearch: http://jblas:[email protected]:8080/mail/inbox
//  obj.domain:       http://jblas:[email protected]:8080
//  obj.protocol:     http:
//  obj.authority:    jblas:[email protected]:8080
//  obj.username:     jblas
//  obj.password:     password
//  obj.host:         mycompany.com:8080
//  obj.hostname:     mycompany.com
//  obj.port:         8080
//  obj.pathname:     /mail/inbox
//  obj.directory:    /mail/
//  obj.filename:     inbox
//  obj.search:       ?msg=1234&type=unread
//  obj.hash:         #msg-content

Your code would change to something like this:

$( '[data-role=page]' ).live( 'pagecreate',function(){
    var $.mobile.path.parseUrl(window.location.href);
    $('#site-toggle').attr('href', obj.protocol + '//' + obj.host + obj.pathname + obj.search + obj.hash); 
});

Documentation can be found here: http://jquerymobile.com/demos/1.0rc2/docs/api/methods.html

Solution 3

Try this:

$.mobile.urlHistory.getActive().url

It should solve your problem.

Share:
17,999
Adi
Author by

Adi

Updated on June 14, 2022

Comments

  • Adi
    Adi almost 2 years

    I am trying to get the current page url in a jquery mobile site. (I am using the default ajax navigation).

    My current attempt is this (binding to pagecreate) but doesnt work on any page injected into the DOM.

    $( '[data-role=page]' ).live( 'pagecreate',function(){
      $('#site-toggle').attr('href',"http://" + location.host.replace('m.','') + window.location.pathname); 
    });
    

    Anyone know what I am missing?

    A.

  • Adi
    Adi over 12 years
    This works on chrome but not my android. Any idea why? $('#site-toggle').live('tap', function () { url = $.mobile.path.parseUrl("http://" + window.location.host.replace('m.','') + window.location.pathname); window.location.href=url.href; });