You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
3.4 KiB
JavaScript
127 lines
3.4 KiB
JavaScript
const linkUrl = window.location.href;
|
|
const links = document.querySelectorAll("a[href=lang]");
|
|
const searchString = "web/";
|
|
const insertion = "zh/";
|
|
const urls = new URL(linkUrl);
|
|
const langTypeParam = urls.searchParams.get("langType");
|
|
const p1 = urls.searchParams.get("param");
|
|
const p2 = urls.searchParams.get("param1");
|
|
const backLink = document.getElementById("goBackLink");
|
|
const subject = document.getElementById("subject");
|
|
const subjectArrZH = ["zh/index", "zh/page-case-study", "zh/page-contact"];
|
|
const subjectArrEN = ["en/index", "en/page-case-study", "en/page-contact"];
|
|
const titleArrZH = [
|
|
"首页",
|
|
["古典橱柜", "现代橱柜"],
|
|
"浴室柜",
|
|
"衣柜",
|
|
// ["实木门", "复合板门"],
|
|
["床", "床垫"],
|
|
// ["椅子", "组合"],
|
|
["客厅", "餐厅"],
|
|
"联系我们",
|
|
];
|
|
const titleArrEN = [
|
|
"Home",
|
|
["Classical Kitchen/Matt Flooring", "Modern Kitchen/Polished Flooring"],
|
|
"Vanity/Toilet",
|
|
"Closet",
|
|
// ["Solidwood Door", "Composite Wood Door"],
|
|
["Bed", "Mattress"],
|
|
// ["Chair", "Combination"],
|
|
["Living Room", "Dining Room"],
|
|
"Contact Us",
|
|
];
|
|
|
|
// 遍历所有符合条件的链接元素
|
|
links.forEach(function (link) {
|
|
console.log(link);
|
|
// 输出符合条件的链接元素的 href 属性值
|
|
link.addEventListener("click", function (event) {
|
|
event.preventDefault();
|
|
const content = link.textContent;
|
|
let url = "";
|
|
if (content == "中文") {
|
|
url = insertLang(linkUrl.toString(), 1);
|
|
if (langTypeParam == 2) {
|
|
url = urlUpdate(url.toString(), "langType", 1);
|
|
}
|
|
} else {
|
|
url = insertLang(linkUrl.toString(), 2);
|
|
if (langTypeParam == 1) {
|
|
url = urlUpdate(url.toString(), "langType", 2);
|
|
}
|
|
}
|
|
window.location.href = url;
|
|
});
|
|
});
|
|
|
|
// 插入路由
|
|
function insertLang(url, lang) {
|
|
if (lang == 1) {
|
|
if (!url.includes("zh") && !url.includes("en")) {
|
|
const position = url.indexOf(searchString) + searchString.length;
|
|
const modifiedString =
|
|
url.slice(0, position) + insertion + url.slice(position);
|
|
return modifiedString;
|
|
}
|
|
return url.replace("en", "zh");
|
|
} else {
|
|
return url.replace("zh", "en");
|
|
}
|
|
}
|
|
|
|
function urlUpdate(url, param, value) {
|
|
return url.replace(
|
|
new RegExp("([?&])" + param + "=.*?(&|$)"),
|
|
"$1" + param + "=" + value + "$2"
|
|
);
|
|
}
|
|
|
|
// 标题
|
|
function createSubject() {
|
|
let title = "";
|
|
if (linkUrl.toString().includes("zh")) {
|
|
if (linkUrl.toString().includes(subjectArrZH[2])) {
|
|
title = titleArrZH[6];
|
|
console.log(title);
|
|
} else {
|
|
subjectArrZH.forEach((item, index) => {
|
|
if (linkUrl.toString().includes(item)) {
|
|
title = titleArrZH[p1];
|
|
if (Array.isArray(title)) {
|
|
title = title[p2];
|
|
}
|
|
}
|
|
});
|
|
}
|
|
} else {
|
|
if (linkUrl.toString().includes(subjectArrEN[2])) {
|
|
title = titleArrEN[6];
|
|
} else {
|
|
subjectArrEN.forEach((item, index) => {
|
|
if (linkUrl.toString().includes(item)) {
|
|
title = titleArrEN[p1];
|
|
if (Array.isArray(title)) {
|
|
title = title[p2];
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
subject.children[0].innerHTML = title;
|
|
}
|
|
|
|
function init() {
|
|
createSubject();
|
|
// 返回列表
|
|
if (backLink) {
|
|
backLink.addEventListener("click", function (event) {
|
|
event.preventDefault(); // 阻止默认链接跳转行为
|
|
window.history.back(); // 返回上一级
|
|
});
|
|
}
|
|
}
|
|
|
|
init();
|