How to get a NON-standard attribute in IE8 through javascript?
I have an HTML page that has this doctype:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
However, the HTML contains this tag:
<applet src="blahblah"></applet>
(EDIT: actually the HTML doesn't contain the applet. The applet is created dynamically by other javascript code).
Yes, I know that applet
is deprecated, and I know that an applet
tag can't contain a src
attribute, but I can't edit that HTML code.
The problem is this Javascript code:
alert(appletElement.getAttribute('src'));
In FF and Chrome it shows "blahblah", but in IE8 it shows null
. Also,开发者_JAVA百科 appletElement.attributes['src']
is not defined.
Anybody knows how to get the src
attribute in IE8 with strict mode?
Thanks
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<title>Test Case</title>
<applet id="myapplet" src="blahblah"></applet>
<script>
var aplt = document.getElementById('myapplet');
alert(aplt.getAttribute('src'));
</script>
Works for me in IE8.
Have you tried
appletElement.src
I have found a solution.
Instead of using document.createElement
to create those applets dynamically, I use this function:
function wrs_createElement(elementName, attributes, creator) {
if (attributes === undefined) {
attributes = {};
}
if (creator === undefined) {
creator = document;
}
var element;
/*
* Internet Explorer fix:
* If you create a new object dynamically, you can't set a non-standard attribute.
* For example, you can't set the "src" attribute on an "applet" object.
* Other browsers will throw an exception and will run the standard code.
*/
try {
var html = '<' + elementName + ' ';
for (var attributeName in attributes) {
html += attributeName + '="' + attributes[attributeName].split('"').join('\\"') + '" ';
}
html += '>';
element = creator.createElement(html);
}
catch (e) {
element = creator.createElement(elementName);
for (var attributeName in attributes) {
element.setAttribute(attributeName, attributes[attributeName]);
}
}
return element;
}
精彩评论