如何在移动设备加载特定链接时发送警报?

huangapple go评论71阅读模式
英文:

How can I send an alert when this specific link is loaded on mobile?

问题

以下是翻译好的代码部分,不包含其他内容:

window.onload = function () {
  if (document.url == "https://tfojofficialwebsite.glitch.me/videos.html") {
    if (
      navigator.userAgent.match(/Android/i) ||
      navigator.userAgent.match(/webOS/i) ||
      navigator.userAgent.match(/iPhone/i) ||
      navigator.userAgent.match(/iPad/i) ||
      navigator.userAgent.match(/iPod/i) ||
      navigator.userAgent.match(/BlackBerry/i) ||
      navigator.userAgent.match(/Windows Phone/i)
    ) {
      console.log("true");
      alert("This page may not work properly on your device.");
    } else {
      console.log("false");
    }
  }
};

这段代码的目的是在移动设备上加载视频页面时显示一个警告,但你可能犯了一个错误。document.url 不是正确的属性来获取当前页面的 URL。你应该使用 window.location.href 来获取当前页面的 URL。所以,将 document.url 更改为 window.location.href 可能会修复问题。修复后的代码如下:

window.onload = function () {
  if (window.location.href == "https://tfojofficialwebsite.glitch.me/videos.html") {
    if (
      navigator.userAgent.match(/Android/i) ||
      navigator.userAgent.match(/webOS/i) ||
      navigator.userAgent.match(/iPhone/i) ||
      navigator.userAgent.match(/iPad/i) ||
      navigator.userAgent.match(/iPod/i) ||
      navigator.userAgent.match(/BlackBerry/i) ||
      navigator.userAgent.match(/Windows Phone/i)
    ) {
      console.log("true");
      alert("This page may not work properly on your device.");
    } else {
      console.log("false");
    }
  }
};

这应该会在移动设备上加载视频页面时显示警告。希望这可以帮助你解决问题。

英文:

I'm making a website for a small game I'm working on. And the website's index page can be found here. The videos page doesn't work properly on mobile, and I want to make an alert show up when the videos page is loaded.

Here's what I had:

window.onload = function () {
      if (document.url == "https://tfojofficialwebsite.glitch.me/videos.html") {
    if (
      navigator.userAgent.match(/Android/i) ||
      navigator.userAgent.match(/webOS/i) ||
      navigator.userAgent.match(/iPhone/i) ||
      navigator.userAgent.match(/iPad/i) ||
      navigator.userAgent.match(/iPod/i) ||
      navigator.userAgent.match(/BlackBerry/i) ||
      navigator.userAgent.match(/Windows Phone/i)
    ) {
      console.log("true");
      alert("This page may not work properly on your device.");
    } else {
      console.log("false");
    }
  }
};

And what I was expecting was an alert when the page is loaded on mobile, but it doesn't do anything, mobile or not. What did I mess up?

答案1

得分: 1

我注意到你的代码没有正常运行。问题似乎出在使用

document.url

这个属性是未定义的,但你可以通过使用

if (window.location.href == "https://tfojofficialwebsite.glitch.me/videos.html") 

来实现所需的结果。这是一个简单的更改,应该可以解决你代码的问题。

英文:

I noticed that your code is not functioning properly. The issue seems to be with the use of

document.url

This property is undefined, but you can achieve the desired result by using

if (window.location.href == "https://tfojofficialwebsite.glitch.me/videos.html") 

This is a simple change, and it should resolve the problem with your code.

huangapple
  • 本文由 发表于 2023年2月10日 06:52:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定