Results 1 to 1 of 1

Thread: Switch Menu - alternative for document.write() and listener

  1. #1
    Join Date
    May 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Switch Menu - alternative for document.write() and listener

    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.
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	2022-05-18 14_13_52-PageSpeed Insights.jpg 
Views:	189 
Size:	16.2 KB 
ID:	6506  

Similar Threads

  1. Switch Menu - alternative for document.write() and listener
    By leroux in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 05-20-2022, 11:03 PM
  2. document.getElementById alternative
    By tochrene in forum JavaScript
    Replies: 1
    Last Post: 10-24-2013, 07:01 AM
  3. Resolved using document.write to write a script tag
    By traq in forum JavaScript
    Replies: 4
    Last Post: 11-16-2010, 04:28 AM
  4. Replies: 0
    Last Post: 05-03-2010, 03:03 PM
  5. DOM alternative to document.write(xmlhttp.responseText)
    By molendijk in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 08-21-2008, 11:27 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •