Possible Duplicate:
JavaScript works on Safari 3 but not on newer versions
The following JavaScript is working with IE but not with Safari/Firefox:
function loadframe() {
var iframeEl = document.getElementById("ifrm");
if (!iframeEl) {
var el = document.createElement('<iframe id="ifrm" width="80" height="80" src=\'<!--EP CLASS="com.epiphany.presentation.ServerURLWriter" ACTION="campaign_segment_load" TEMPLATE="campaign\\campaign_segments" -->\' onload="dw_display(\'ifrm\',\'segmentspn\')" ></iframe>');
document.body.appendChild(el);
return true;
}
return false;
}
Can anybody spot the开发者_JAVA百科 problem?
It's just because you are using it wrong.
document.createElement
expects a tag name not html code:
iframe = document.createElement("iframe");
https://developer.mozilla.org/en/DOM/document.createElement
http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-2141741547
Dom 2 Core Spec:
createElement
tagName of type DOMString The name of the element type to instantiate.
The ability to pass a HTML string to document.createElement()
is an Internet Explorer proprietary feature. Other browsers do not allow you to do this.
One possible solution would be to create a wrapper element, and set it's innerHTML
to the HTML string you have there.
function loadframe() {
var iframeEl = document.getElementById("ifrm");
if (!iframeEl) {
var el = document.createElement('div');
el.innerHTML = '<iframe id="ifrm" width="80" height="80" src=\'<!--EP CLASS="com.epiphany.presentation.ServerURLWriter" ACTION="campaign_segment_load" TEMPLATE="campaign\\campaign_segments" -->\' onload="dw_display(\'ifrm\',\'segmentspn\')" ></iframe>';
document.body.appendChild(el);
return true;
}
return false;
}
Note that your HTML is also invalid — you cannot have a HTML comment inside an attribute.
The code is not valid. The parameter for document.createElement can only be the tagname, like "iframe". You then have to set the attributes later.
document.createElement('iframe');
It doesn't work in standards-compliant browsers and won't work in Internet Explorer 9. See this link:
http://msdn.microsoft.com/en-us/library/ff986077%28v=vs.85%29.aspx
精彩评论