“The editor has encountered an unexpected error” After add defer tag to java script

I added below script to add defer tag in javascripts. Once it added to function.php
Gets error on create post “The editor has encountered an unexpected error.” I cannot create or edit post.
How can i add defer tag to javascripts without affecting to the editor. I don’t have mush experience in coding. Thanks.

Conditionally Loading JavaScript/CSS for Shortcodes

I released a plugin that creates a shortcode and requires a JavaScript file and a CSS file to load on any page that contains that shortcode. I could just make the script/style load on all pages, but that’s not the best practice. I only want to load the files on pages that call the shortcode. I’ve found two methods of doing this, but both have problems.