Actually, as your code that empty the script
tag at the end of your HTML page is above it, it runs the code before the script is fully loaded. That is the reason why you get the error:
Uncaught TypeError: Cannot set property 'innerHTML' of null
In order to run the code after everything is loaded on the webpage, use:
document.addEventListener("DOMContentLoaded", function(event) {});
It will run the code after all DOM is loaded.
See the example below, the first example doesn't use the document.addEventListener("DOMContentLoaded", function(event) {});
and the second one using it.
Wrong example:
<script>
document.getElementById('wk-ra-state').innerHTML = '';
</script>
<!-- end of the html page -->
<script id="wk-ra-state" type="application/json">{&q;APP_SERIALIZATION_KEY&q;:{&q;recentFavorites&q;:{&q;pending&q;:false,&q;recentItems&q;:[{&q;id&q;:&q;&q;,&q;viewed&q;:false,&q;type&q;:&q;document&q;,&q;title&q;:&q;&q;,&q;link&q;:&q;javascript:void(0)&q;}]},&q;suggestions&q;:{&q;pending&q;:false,&q;requests&q;:{},&q;cach ............ </script>
Good example:
<script>
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById('wk-ra-state').innerHTML = '';
});
</script>
<!-- end of the html page -->
<script id="wk-ra-state" type="application/json">{&q;APP_SERIALIZATION_KEY&q;:{&q;recentFavorites&q;:{&q;pending&q;:false,&q;recentItems&q;:[{&q;id&q;:&q;&q;,&q;viewed&q;:false,&q;type&q;:&q;document&q;,&q;title&q;:&q;&q;,&q;link&q;:&q;javascript:void(0)&q;}]},&q;suggestions&q;:{&q;pending&q;:false,&q;requests&q;:{},&q;cach ............ </script>