0

我目前有以下内容:

$ul.append('<li>' + 
'<a href="setlocale.aspx?returnURL=default.aspx&localesetting=' + 
this.slice(0) + '">' + this.slice(3) + '</a>');

假设我的网址以:/c-577-camping.aspx

如何在我的 jquery 中使用这个值?它应该更改default.aspxc-577-camping.aspx,如果网站是,www.site.com/c-517-cookers.aspx那么它应该附加:

http://www.site.com/c-517-cookers.aspx?setlocale.aspx?returnURL=c-517-cookers.aspx&localesetting=fr-FR

4

3 回答 3

1

要获取路径,可以使用 window.location.pathname:

$(document).ready(function() {
  var pathname = window.location.pathname;

  $ul.append('<li>' + '<a href="setlocale.aspx?returnURL=' + pathname + '&localesetting=' + this.slice(0) + '">' + this.slice(3) + '</a>');
});
于 2012-07-31T12:30:26.060 回答
1

location.pathname包括斜线,所以试试:

'<a href="setlocale.aspx?returnURL='+ location.pathname.replace(/\//g,'') +'&localesetting=' +....

如果路径名有可能已经包含一些获取参数,您将需要使用encodeURI(location.pathname.replace(/^\//,''),仅替换第一个斜杠或使用location.pathname.replace(/\//g,'').split('?')[0]来切断参数。

PS:不要用window.location.pathname,没必要。实际上,不使用会稍微快一点window,因为它是对全局(无名)对象的循环引用。

于 2012-07-31T12:42:07.207 回答
0

怎么用

window.location.pathname

?

所以完整的代码:

$ul.append('<li>' + '<a href="setlocale.aspx?returnURL=' + window.location.pathname + '&localesetting=' + this.slice(0) + '">' + this.slice(3) + '</a>');
于 2012-07-31T12:30:06.437 回答