diff --git a/dom/html/HTMLObjectElement.cpp b/dom/html/HTMLObjectElement.cpp
index e0e79b69af..a0d06a8c65 100644
--- a/dom/html/HTMLObjectElement.cpp
+++ b/dom/html/HTMLObjectElement.cpp
@@ -193,7 +193,13 @@ HTMLObjectElement::AfterMaybeChangeAttr(int32_t aNamespaceID, nsIAtom* aName,
// attributes before inserting the node into the document.
if (aNotify && IsInComposedDoc() && mIsDoneAddingChildren &&
aName == nsGkAtoms::data) {
- return LoadObject(aNotify, true);
+ nsContentUtils::AddScriptRunner(NS_NewRunnableFunction(
+ [self = RefPtr(this), aNotify]() {
+ if (self->IsInComposedDoc()) {
+ self->LoadObject(aNotify, true);
+ }
+ }));
+ return NS_OK;
}
}
diff --git a/dom/html/HTMLSharedObjectElement.cpp b/dom/html/HTMLSharedObjectElement.cpp
index f0cf4c188c..d7eec215a0 100644
--- a/dom/html/HTMLSharedObjectElement.cpp
+++ b/dom/html/HTMLSharedObjectElement.cpp
@@ -177,8 +177,13 @@ HTMLSharedObjectElement::AfterMaybeChangeAttr(int32_t aNamespaceID,
// a document, just in case that the caller wants to set additional
// attributes before inserting the node into the document.
if (aNotify && IsInComposedDoc() && mIsDoneAddingChildren) {
- nsresult rv = LoadObject(aNotify, true);
- NS_ENSURE_SUCCESS(rv, rv);
+ nsContentUtils::AddScriptRunner(NS_NewRunnableFunction(
+ [self = RefPtr(this), aNotify]() {
+ if (self->IsInComposedDoc()) {
+ self->LoadObject(aNotify, true);
+ }
+ }));
+ return NS_OK;
}
}
}