【问题标题】:can I still optimize my JS so that it is faster我还能优化我的 JS 让它更快吗
【发布时间】:2022-01-16 07:12:04
【问题描述】:

有效!

你不会相信那有多烦人。

这是工作代码,有人可以帮我优化代码吗?

有没有什么技巧可以缩短 js 或者让它更快? (网站也跑pagespeed,js已经优化了,还是有点慢)

JS:

    window.onload = function checkCookie() {
    if (document.cookie.split(';').some((item) => item.trim().startsWith('TechStatCookie='))) {
        document.getElementById("CookiePopup").style.display = "none";
        console.log("TechStatCookie detected ");
    }
    else {
        console.log("TechStatCookie NOT detected ");
    }
};


function _yesCookie() {
    var now = new Date();
    now.setTime(now.getTime() + 2419200000);
    document.cookie = "YesCookie = set; expires=; path=/; SameSite=Strict; Secure" + now.toUTCString();
    document.cookie = "TechStatCookie=1; expires=; path=/; SameSite=Strict; Secure" + now.toUTCString();

    console.log("TechStatCookie set " + now.toUTCString());

    document.cookie = 'NoCookie=; Max-Age=-99999999; path=/; SameSite=Strict; Secure';
    window.ga = window.ga || function() {
        (ga.q = ga.q || []).push(arguments)

    };
    ga.l = +new Date;
    ga('create', 'UA-XXXXXXXX-X', 'auto');
    ga('set', 'anonymizeIp', true);
    ga('send', 'pageview');

    var gascript = document.createElement("script");
    gascript.async = true;
    gascript.src = "https://www.google-analytics.com/analytics.js";
    document.getElementsByTagName("head")[0].appendChild(gascript, document.getElementsByTagName("head")[0]);
    console.log("Tracking enabled! " + now.toUTCString());
    document.getElementById("CookiePopup").style.display = "none";
}

function _noCookie() {
    var now = new Date();
    now.setTime(now.getTime() + 604800000);
    document.cookie = "NoCookie = set; expires=; path=/; SameSite=Strict; Secure" + now.toUTCString();
    document.cookie = "TechStatCookie=1; expires=; path=/; SameSite=Strict; Secure" + now.toUTCString();

    console.log("TechStatCookie set " + now.toUTCString());

    document.cookie = 'YesCookie=; Max-Age=-99999999; path=/; SameSite=Strict; Secure';
    console.log("Tracking disabled... " + now.toUTCString());
    document.getElementById("CookiePopup").style.display = "none";
}

HTML:

    <section id="CookiePopup">
        <br>
        <h1>Datenschutz</h1><br>
        <embed src="https://example.com/update.html" width="100%" height="250px">
        <p>blablabla</p>
        <br>
        <button class="ycookie" onclick='_yesCookie()' title="OK"><i class="far fa-check-circle"></i></button>
        <button class="ncookie" onclick='_noCookie()' title="NEIN"><i class="far fa-times-circle"></i></button>
    </section>

【问题讨论】:

  • 因为您的 function 声明嵌套在另一个范围(try 块)中,它们不会被提升为全局级函数。
  • 要修复它,只需删除try 语句(及其大括号和catch 块) - 或前向声明try 之外的函数并在@987654328 中定义它们@.

标签: javascript html optimization


【解决方案1】:

你好像没有在html中导入js文件。

<script src="./example.js"></script>

【讨论】:

  • 哦,对不起,我忘记了,导入包含在 html 中
  • 那么,你解决了吗?
  • 不是真的 :/ 脚本运行,但是在我得到“TechStatCookie”后也显示了弹出窗口...(所以不应显示弹出窗口)
  • 获取cookie变量: var techStatCookie = new RegExp('(?:^|; )TechStatCookie=([^;]*)').exec(document.cookie);
  • 我已经用“ var techStatCookie = new RegExp('(?:^|; )TechStatCookie=([^;]*)').exec(document.cookie); 试过了,不幸的是它不适用于那个和我的代码“document.cookie =”TechStatCookie = true;到期=格林威治标准时间 9999 年 12 月 31 日星期五 23:59:59; SameSite=严格;安全"; "
【解决方案2】:
TypeError: document.getElementById(...) is null
<anonymous> https://example.com/popup.js:5

对于上述错误,您需要在 HTML 页面底部添加 example.js 脚本标签

Uncaught TypeError: _yesCookie is not a function
onclick https://example.com/:1
example.com:1:1

要修复第二个错误,您需要删除 try & catch 块

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多