2011-11-04 112 views
2

我想在jQuery手机网站中获取当前页面的URL。 (我正在使用默认的ajax导航)。在jQuery手机中获取当前页面的URL

我目前的尝试是这样的(绑定到pagecreate),但在注入DOM的任何页面上都不起作用。

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

任何人都知道我错过了什么?

A.

回答

0

您检查出jQuery-URL-Parser插件?

var url = $.url(); //retrieves current url 

您还可以得到URL的特定部分是这样的:

var file = $.url.attr("file"); 
var path = $.url.attr("path"); 
var host = $.url.attr("host"); 
... 

如果你需要得到查询字符串参数:

var parm = $.url.param("id"); 
4

jQuery Mobile的有一个叫$.mobile.path.parseUrl URL解析方法。它的使用将是这样的:

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

这将返回以下对象:

// 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 

您的代码将改变这样的事情:

$('[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); 
}); 

文档可以在这里找到:http://jquerymobile.com/demos/1.0rc2/docs/api/methods.html

+0

这适用于Chrome但不是我的机器人。任何想法为什么? 。 '$( '#站点切换')生活( '水龙头',函数(){ URL = $ .mobile.path.parseUrl( “HTTP://” + window.location.host.replace(” ';')+ window.location.pathname); \t window.location.href = url.href; });'' – Adi

10

这不是您要寻找的吗?

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

试试这个:

$.mobile.urlHistory.getActive().url 

应该解决您的问题。