在javascript中单击按钮时加载带有路径名的页面
我在这里有点困惑。 我有一个url在javascript中单击按钮时加载带有路径名的页面,javascript,Javascript,我在这里有点困惑。 我有一个urllocahost/product location/agro product,当用户单击此页面上的按钮时,它会将用户带到locahost/product/agro product。经过一些研究,我发现我可以这样更改路径名 函数loadPage(){ var theURL=window.location.pathname; 返回URL.replace(“/product location/”,“/product/”); } 上述方法之所以有效,是因为如果我添加
locahost/product location/agro product
,当用户单击此页面上的按钮时,它会将用户带到locahost/product/agro product
。经过一些研究,我发现我可以这样更改路径名
函数loadPage(){
var theURL=window.location.pathname;
返回URL.replace(“/product location/”,“/product/”);
}
上述方法之所以有效,是因为如果我添加此警报(loadPage())代码>函数外部:它会提醒新的URL路径
现在,我如何从这里编写代码,以便当用户单击按钮时,它将用户带到新的URL?您可以使用window.location.href
:
函数加载页(){
var theURL=window.location.pathname;
var newURL=theURL.replace(“/product location/”,“/product/”);
//设置URL
window.location.href=newURL;
}
您可以使用window.location.href
:
函数加载页(){
var theURL=window.location.pathname;
var newURL=theURL.replace(“/product location/”,“/product/”);
//设置URL
window.location.href=newURL;
}
好的,明白了。只是需要做一些修改
function loadPage(){
var theURL = window.location.href;
return window.location = theURL.replace("/product-location/", "/product/");
//Set URL
}
好的,明白了。只是需要做一些修改
function loadPage(){
var theURL = window.location.href;
return window.location = theURL.replace("/product-location/", "/product/");
//Set URL
}
我建议使用,因为您可以选择它打开的窗口(更不用说各种不同的选项)
您的按钮HTML如下所示:
<button onclick="loadPage();">Visit new page</button>
访问新页面
我建议使用,因为您可以选择它打开的窗口(更不用说各种不同的选项)
您的按钮HTML如下所示:
<button onclick="loadPage();">Visit new page</button>
访问新页面
通常,当您想到客户机-服务器设置时,会发生这种类型的交互。页面上的链接由服务器操作,服务器提供新页面。你想的纯粹是客户。有往返。Http请求/响应等。当url更改时,服务器将提供一个新页面。您可能还想了解单页应用程序的工作原理。通常,当您想到客户机-服务器设置时,就会发生这种类型的交互。页面上的链接由服务器操作,服务器提供新页面。你想的纯粹是客户。有往返。Http请求/响应等。当url更改时,服务器将提供一个新页面。您可能还想了解单页应用程序的工作原理。