Javascript 更新URL onClick

Javascript 更新URL onClick,javascript,jquery,Javascript,Jquery,我正在尝试更新我的单击()函数中的当前URL var str = window.location; // http://localhost:8888/000D6766F2F6/10001/edit/private/basic var new_str = str.replace("basic", "a"); alert(new_str); location.href = new_str; 我一直在 我在这里做了不该做的事吗 你很接近。您的replace语句过早地更改了url var new

我正在尝试更新我的单击()函数中的当前URL

var str = window.location; // http://localhost:8888/000D6766F2F6/10001/edit/private/basic
var new_str = str.replace("basic", "a");
alert(new_str);
location.href = new_str;

我一直在



我在这里做了不该做的事吗

你很接近。您的replace语句过早地更改了url

var new_str = window.location.href.replace("basic", "a");
alert(new_str);
window.location.href = new_str;

你很接近。您的replace语句过早地更改了url

var new_str = window.location.href.replace("basic", "a");
alert(new_str);
window.location.href = new_str;

您需要从window.location获取属性“href”:

var str = window.location.href; // << Get href
var new_str = str.replace("basic", "a");
alert(new_str);
location.href = new_str;

var str=window.location.href;// 您需要从window.location获取属性“href”:

var str = window.location.href; // << Get href
var new_str = str.replace("basic", "a");
alert(new_str);
location.href = new_str;

var str=window.location.href;// 
window.location
不是字符串,它只是一个字符串表示形式。因此,您调用的
.replace
方法不是
字符串
.replace
方法。事实上,它是一个导航到新页面而不添加新的历史记录条目,并且不返回任何未定义的内容

如果将其转换为
字符串
(或访问等效的
.href
属性),则代码将正常工作:

var str = String(window.location); // http://localhost:8888/000D6766F2F6/10001/edit/private/basic
var new_str = str.replace("basic", "a");
alert(new_str);
location.href = new_str;

window.location
不是字符串,它只是一个字符串表示形式。因此,您调用的
.replace
方法不是
字符串
.replace
方法。事实上,它是一个导航到新页面而不添加新的历史记录条目,并且不返回任何未定义的内容

如果将其转换为
字符串
(或访问等效的
.href
属性),则代码将正常工作:

var str = String(window.location); // http://localhost:8888/000D6766F2F6/10001/edit/private/basic
var new_str = str.replace("basic", "a");
alert(new_str);
location.href = new_str;