V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
CSGO
V2EX  ›  问与答

如何让自己网页的 js 手机端打开每次都生效?

  •  
  •   CSGO · 2019-03-29 09:47:49 +08:00 · 1399 次点击
    这是一个创建于 1853 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我网络上找了个 js 代码:

    window.onload = function () { var wifitimecss = new Date().getHours(); if (wifitimecss > 6 && wifitimecss < 18) { document.getElementById("wifitimecss").setAttribute("href", "/index.css") } else { document.getElementById("wifitimecss").setAttribute("href", "/index-night.css") } }

    就是根据系统时间,当系统时间处于 18 点到早晨 6 点,会加载一个 index-night.css ,达到夜间模式。 但是今天试了下,pc 都没问题,手机打开,如果是夜间模式期间打开,即使现在 9 点了,打开依然是夜间的 css,只有在隐私模式下打开才会是正常的 css。这是为什么呢?应该如何解决?

    7 条回复    2019-03-29 16:45:21 +08:00
    ismumu
        1
    ismumu  
       2019-03-29 10:23:26 +08:00
    setAttribute("href", "/index.css" + Date.now())

    嗯,粗暴一点
    ismumu
        2
    ismumu  
       2019-03-29 10:26:06 +08:00   ❤️ 1
    setAttribute("href", "/index.css?t=" + Date.now())
    CSGO
        3
    CSGO  
    OP
       2019-03-29 11:19:46 +08:00
    @ismumu 这是要加在哪里呢?
    CSGO
        4
    CSGO  
    OP
       2019-03-29 11:20:14 +08:00
    @ismumu 是代替原本那一段吗?
    gftfl
        5
    gftfl  
       2019-03-29 12:59:16 +08:00
    day = new Date()
    hr = day.getHours()
    if (hr >18 && hr<6) {
    document.write("<link rel='stylesheet' type='text/css' href='/index-night.css'/>") } else {
    document.write("<link rel='stylesheet' type='text/css' href='/index.css'/>")
    }
    xingyue
        6
    xingyue  
       2019-03-29 13:15:41 +08:00
    我猜:因为你二次进入页面的时候,onload 事件没触发,pc 上 chrome 貌似根据时间变化会自动重加载一次页面而手机不一定。解决方案是:加一个 window.onfocus = function(){do sth;}
    zhtttyecho
        7
    zhtttyecho  
       2019-03-29 16:45:21 +08:00
    缓存?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5393 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 06:53 · PVG 14:53 · LAX 23:53 · JFK 02:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.