Reputation: 4022
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.
Upvotes: 2
Views: 18052
Reputation: 311
Maybe you want this
$(location).attr('pathname');
You should read this article
Upvotes: 0
Reputation: 11
Try this:
$.mobile.urlHistory.getActive().url
It should solve your problem.
Upvotes: 1
Reputation: 3612
Isn't this you are looking for:
var currentUrl = $.mobile.activePage.data('url');
Upvotes: 11
Reputation: 76003
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
Upvotes: 4
Reputation: 46047
Have you checked out the jQuery-URL-Parser plugin?
var url = $.url(); //retrieves current url
You can also get specific parts of the URL like this:
var file = $.url.attr("file");
var path = $.url.attr("path");
var host = $.url.attr("host");
...
If you need to get Querystring parameters:
var parm = $.url.param("id");
Upvotes: 0