1) Script Title:
switch menu
2) Script URL (on DD):
http://www.dynamicdrive.com/dynamici...switchmenu.htm
3) Describe problem:
Hi, please I am dealing with core vitals in google search console where they reporting me two problems with DD script:
Attachment 6507
1) Avoid document.write()
For users on slow connections, external scripts dynamically injected via `document.write()` can delay page load by tens of seconds. Learn more.
Source
/js/switchmenu.js:11:9
/js/switchmenu.js:12:9
/js/switchmenu.js:13:9
- is possible use different approach for these three document.write()?
Code:
if (document.getElementById){ //DynamicDrive.com change
document.write('<style type="text/css">\n')
document.write('.submenu{display: none;}\n')
document.write('</style>\n')
}
2) Registers an unload listener
The `unload` event does not fire reliably and listening for it can prevent browser optimizations like the Back-Forward Cache. Use `pagehide` or `visibilitychange` events instead. Learn more
/js/switchmenu.js:56:22
- line 56 is this function function savemenustate(), can I improve it?
Code:
function savemenustate(){
var inc=1, blockid=""
while (document.getElementById("sub"+inc)){
if (document.getElementById("sub"+inc).style.display=="block"){
blockid="sub"+inc
break
}
inc++
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
document.cookie=cookiename+"="+cookievalue
}
if (window.addEventListener)
window.addEventListener("load", onloadfunction, false)
else if (window.attachEvent)
window.attachEvent("onload", onloadfunction)
else if (document.getElementById)
window.onload=onloadfunction
if (persistmenu=="yes" && document.getElementById)
window.onunload=savemenustate
-------------
my code generate many (sometimes 50 for example) of these switch menus for my events, so it can slow my website significantly in eyes of google's PageSpeed tool. I am not good in JS, but need to fix it.
Thank you.
Bookmarks