views:

42

answers:

3

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"&gt;

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, appletElement.attributes['src'] is not defined.

Anybody knows how to get the src attribute in IE8 with strict mode?

Thanks

+1  A: 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
                               "http://www.w3.org/TR/html4/strict.dtd"&gt;
<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.

Alohci
Ok, I was not sincere in my question. The applet object is not inserted in the HTML, is created dynamically with document.createElement. When you set a not standard attribute to that applet, you can't retrieve it later. It returns `null` ever.
Nitz
A: 

Have you tried

appletElement.src
Šime Vidas
It doesn't work :(
Nitz
A: 

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;
}
Nitz