[英]iOS Safari Javascript not working but works on Desktop and Android
The purpose of this script is to hide and show figure elements depending on a query string.此脚本的目的是根据查询字符串隐藏和显示图形元素。 eg, website.com/repair-catalog?catalog=brand1.
例如, website.com/repair-catalog?catalog=brand1。
This script works on Desktop and from what I've seen Android devices.这个脚本适用于桌面和我所看到的 Android 设备。 But on iOS Safari, the script doesn't seem to work.
但是在 iOS Safari 上,该脚本似乎不起作用。
If I did visited: website.com/repair-catalog?catalog=brand1
.如果我确实访问过:
website.com/repair-catalog?catalog=brand1
。
the case "brand1"
would kick off and would proceed to show only figures with innerText of Headphones, earbuds, and speakers. case "brand1"
将开始,并将继续仅显示带有耳机、耳塞和扬声器的case "brand1"
数字。 And hides the other figures.并隐藏其他人影。 Once again, this works in Desktop and Android.
再一次,这适用于桌面和 Android。 But when I visit the page on iOS Safari, the
case "brand1"
is running, but instead of showing Headphones, earbuds, and speakers... the script automatically goes into the default
case.但是当我在 iOS Safari 上访问该页面时,
case "brand1"
正在运行,而不是显示耳机、耳塞和扬声器......脚本会自动进入default
案例。
Why doesn't this script work on Safari iOS as it does in Desktop and Android?为什么此脚本在 Safari iOS 上不能像在桌面和 Android 中那样工作? It shouldn't hide all the figures.
它不应该隐藏所有数字。 Only the figures that don't match within the cases.
只有在案例中不匹配的数字。
I've attempted to rewrite my script so that it doesn't use URLSearchParams
, switch statements
, and forEach
loops.我试图重写我的脚本,以便它不使用
URLSearchParams
、 switch statements
和forEach
循环。 But still I end up having the same result of it working on Desktop and Android but not Safari iOS.但是我最终在桌面和 Android 上得到了相同的结果,但在 Safari iOS 上却没有。 On Safari iOS, default case is ran all the time.
在 Safari iOS 上,默认情况一直在运行。
Note, I've checked the typeof figure.innerText
and it is a string, which means it should match case "brand1"
.请注意,我已经检查了
typeof figure.innerText
并且它是一个字符串,这意味着它应该匹配case "brand1"
。 It does this correctly on Desktop and android.它在桌面和 android 上正确执行此操作。
const catalogQuery = new URLSearchParams(document.location.search);
const catalogType = catalogQuery.get("catalog")
const nodeList = document.querySelectorAll(".figure-wrapper a figure");
const x = [...nodeList];
try {
if (catalogType !== null) {
switch (catalogType) {
case "brand1":
x.forEach(figure => {
switch (figure.innerText) {
case "HEADPHONES":
figure.style.display = "block";
figure.parentElement.attributes[0].value = "/headphones";
break;
case "EARBUDS":
figure.style.display = "block";
figure.parentElement.attributes[0].value = "/earbuds";
break;
case "SPEAKERS":
figure.style.display = "block";
figure.parentElement.attributes[0].value = "/speakers";
break;
default:
figure.parentElement.style.display = "none";
break;
}
});
break;
default:
location.href = "/repair-catalog";
break;
}
}
} catch (err) {
console.log(err);
}
URLSearchParams
is not supported in all versions of safari or older browsers.并非所有版本的 safari 或更旧的浏览器都支持
URLSearchParams
。
please use a polyfill or split the url params and use the query param values .请使用 polyfill 或拆分 url 参数并使用查询参数值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.