please convert this from being dependant on jquery to vanilla Javascript, maintaining…
- User
please convert this from being dependant on jquery to vanilla Javascript, maintaining the exact same functionality if( typeof google !== 'undefined' ) // did Google's JavaScript load? { google.maps.event.addDomListener( window, 'load', initialise ); } else { ( document ).ready( function() { ( '#map-canvas' ).addClass( 'hidden' ); $( '#broken-map' ).removeClass( 'hidden' ); } ); }
- Assistant
Here's the conversion of the code from jQuery to vanilla JavaScript:
javascriptif (typeof google !== 'undefined') { // did Google's JavaScript load? google.maps.event.addDomListener(window, 'load', initialise); } else { document.addEventListener('DOMContentLoaded', function () { document.querySelector('#map-canvas').classList.add('hidden'); document.querySelector('#broken-map').classList.remove('hidden'); }); }The code checks if
googleis defined, and if it is, it adds aloadevent listener to thewindowthat calls theinitialisefunction when the page has finished loading. Otherwise, it waits for theDOMContentLoadedevent and adds a listener to it that hides themap-canvaselement and shows thebroken-mapelement by manipulating their CSS classes.