Woes of Safari's Javascript with [removed]

2019-05-16 11:32发布

问题:

My problem only happens on Safari. IE, FF, Chrome and Opera all work flawlessly. I am adding an object to the DOM (exactly the same way YouTube does it, depending on ActiveX or NPAPI) so after I determine the write object type I add it to the DOM via :

document.write(MyObject)

At the head section of the page, so that js functions called from body can access it. It works on everything except Safari. The only way I can let Safari work is by adding an alert after document.write! I even tried setTimeout, but it works one time and fails 10.

回答1:

Have you considered using document.body.appendChild(MyObject)?



回答2:

Document.write doesn't work correctly in the body itself, and document.body.appendChild to add an object tag doesn't work.

So what I did was

var objectContainer = createElement("div");
objectContainer.innerHTML = "<object blah blah> <\/object>"; //<--notice the escaping of /
document.body.appendChild(objectContainer);

And now it works everywhere.