如何在 javascript 或 php 中编辑 url 路径
how to edit url path in javascript or php
我想编辑当前页面的 URL 路径,然后将浏览器重定向到新路径
例如我有这个 URL https://example.com/about
我想在按下按钮时获取当前 URL
并使 https://example.com/en/about
所以会喜欢这个
var url = window.location.href;
// then edit it so it will be like
// https://example.com/en/about
window.location.replace(new_url);
如果我想支持主url
,我该如何删除en
如何在纯 Javascript 或 jQuery 中执行此操作
或者如果在 php
中有办法做到这一点
提前致谢... <3
使用 window.location.pathname
是 在 中搜索 的最佳价值 url 的路径替换为什么你要
function replace_path_in_url_with(path) {
var url = window.location.href.replace(window.location.pathname, "/"+path);
window.location.replace(url);
}
replace_path_in_url_with("en/about");
// and if you want to go back just do
replace_path_in_url_with("about");
您可以轻松地将 window.location
分解成多个部分
此功能允许您传入一种语言并将页面位置更改为同一路径下的语言
function ChangeLocation(lang) {
window.location.href = window.location.protocol + "//" + window.location.hostname + "/" + lang + window.location.pathname;
}
话虽如此,我建议您也看看 .htaccess
,因为评论中有很多人对此表示支持
我想编辑当前页面的 URL 路径,然后将浏览器重定向到新路径
例如我有这个 URL https://example.com/about
我想在按下按钮时获取当前 URL
并使 https://example.com/en/about
所以会喜欢这个
var url = window.location.href;
// then edit it so it will be like
// https://example.com/en/about
window.location.replace(new_url);
如果我想支持主url
,我该如何删除en如何在纯 Javascript 或 jQuery 中执行此操作 或者如果在 php
中有办法做到这一点提前致谢... <3
使用 window.location.pathname
是 在 中搜索 的最佳价值 url 的路径替换为什么你要
function replace_path_in_url_with(path) {
var url = window.location.href.replace(window.location.pathname, "/"+path);
window.location.replace(url);
}
replace_path_in_url_with("en/about");
// and if you want to go back just do
replace_path_in_url_with("about");
您可以轻松地将 window.location
分解成多个部分
此功能允许您传入一种语言并将页面位置更改为同一路径下的语言
function ChangeLocation(lang) {
window.location.href = window.location.protocol + "//" + window.location.hostname + "/" + lang + window.location.pathname;
}
话虽如此,我建议您也看看 .htaccess
,因为评论中有很多人对此表示支持