I am using JavaScript to load dynamic PDF content into an <embed>
element:
document.querySelector('button').addEventListener("click", (event) => {
document.querySelector('embed').src = 'https://example.com/';
});
<div id="embed-container">
<embed>
</div>
<button>Load Embed Content</button>
How can I detect when it finished loading? You can see in the devtools that when the button is clicked, there’s a pending GET request to that URL. In my case it takes a few seconds to load so I want to display a loading animation and once it finished pending and I get 200
then show the <embed>
(Instead of displaying a blank page for a few seconds)
2
Answers
You can hide the element with
display: none
and use theload
event to make it visible once the content has been loaded.You can create a load event for the embed during the button click and add a loader too: