javascript - 如何在 javascript 或 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
解决方案
You can easily break up window.location
into its parts
This function allows you to pass a language in and changes the page location to that of the language at the same path
function ChangeLocation(lang) {
window.location.href = window.location.protocol + "//" + window.location.hostname + "/" + lang + window.location.pathname;
}
With that being said, I would recommend you also look into .htaccess
as there are a lot of people vouching for it in the comments
推荐阅读
- c# - 具有错误属性类型的模型
- machine-learning - 如何防止 Keras predict_generator 洗牌数据?
- ios - Swift - 从 Cloudkit 公共数据库中获取所有记录
- javascript - 如何使用 React Hooks 处理 React Svg 拖放
- algorithm - 在 2D M x N 网格中查找封闭空间的存在
- air - adobe flash cs6、air 和 flex
- javascript - javascript何时使用箭头函数,何时不使用
- angular - 我在 Angular 4 中为爱好选择了复选框,但我无法获取它的值。我得到空值
- javascript - ajax 函数没有从数据库中填充 HTML 中的表
- d3.js - 无法在 d3.v4.min.js 中添加多个数据集