I am using below code to execute live chat code through which I have added a div and chat box is showing which is working fine.
try {
// LIVECHAT
//if (matchMedia('only screen and (min-width: 1025px)').matches)
//{
var __lc = {};
__lc.license = XXXXXX;
(function () {
var lc = document.createElement("script");
lc.type = "text/javascript";
lc.async = true;
lc.src =
("https:" == document.location.protocol ? "https://" : "http://") +
"cdn.livechatinc.com/tracking.js";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(lc, s);
})();
var LC_API = LC_API || {};
LC_API.open_chat_window = function () {
$(".chatbox").show();
$("#chat-widget-container").show();
};
LC_API.on_chat_window_minimized = function () {
$(".chatbox").show();
$("#chat-widget-container").hide();
};
LC_API.on_chat_window_opened = function () {
$(".chatbox").hide();
$("#chat-widget-container").show();
};
LC_API.on_chat_window_hidden = function () {
$(".chatbox").show();
$("#chat-widget-container").show();
};
//}
} catch (err) {}
$(".openChat").on("click", function (event) {
LC_API.open_chat_window();
return false;
});
But when I go to Google Pagespeed Insights and track the website in mobile https://developers.google.com/speed/pagespeed/insights/?url=https%3A%2F%2Fwww.rosterelf.com%2F, I am getting the low rankings as its keep saying this.
Time to Interactive 11.9 s
If I comment the above code then my percentage getting higher to above 65.
So can someone guide me how can I optimize this script to solve this issue ?
Thanks
2
Answers
Yes you can optimize the script download the cdn link
https://cdn.livechatinc.com/tracking.js
and save in project folderReplace this
then speed of page is increased .
When we load
javascript
on our page then under the hood many things start happening. At first, the browser downloads this file and since it’s a javascript file it starts executing it and stops all other things like parsing the page further.In your case, the same thing is happening and it is increasing the loading time for your page.
Also, you can see the
dnslookup
is taking so much time so you have improved this too. Now follow the given steps to improve your loading time.Steps:
preconnect
ordns-prefetch
resource hints to establish early connections to important third-party origins.defer
andasync
during loading your javascript files so that js execution doesn’t affect page loading.<link rel="preload" as="script" href="critical.js">
Follow the above steps to improve your page performance affected by javascript
Some more tips to increase your page speed:
CSS
andJS
cache-policy
. This will help you get static resources from the cache and hence improve the loading time.Most Important tip –
From the URL I can see that your website is vulnerable to XSS attacks. So please look up into that and also work on your security headers like
Content Security Policy (CSP)
.For more examples see this website – https://codebulbs.com and check its source. You will get many things to learn from the source of this website.