Only insert JS if contenttype and record ID are valid and defer loading of library until page load is complete

Only insert JS if contenttype and record ID are valid and defer loading of library until page load is complete