ipradhansk
ipradhansk

Reputation: 352

Unable to set 'data' attribute for 'Object' tag in jQuery. [IE8 only]

I am creating an object element dynamically in jQuery, to render some content. It works in all browsers except IE8.

The code:

    j$(document).ready(function(){ 
        j$('.objContainer').html(j$('<object>').attr(
                            {'data':'http://www.stackoverflow.com',
                             'type':'text/html'}));
    });

The HTML structure created after the execution(in IE8):

    <object type="text/html"></object>

In other browsers[IE9, Firefox, Chrome]:

    <object data="http://www.stackoverflow.com" type="text/html"></object>

Any solutions?

Upvotes: 6

Views: 10058

Answers (3)

Ahmed El Kilani
Ahmed El Kilani

Reputation: 345

It should work fine, Though i recommend you use $.data() method

http://api.jquery.com/jQuery.data/

It is much safer, and jQuery ensures that the data is removed when DOM elements are removed via jQuery methods.

Example:

<object id='myObj' data-url="http://www.stackoverflow.com" type="text/html"></object>

And you can read the value like:

var url = $('#myObj').data('url');// Read the value
$('#myObj').data('url', 'some-other-value');// Set a new value

Upvotes: 1

jdigital
jdigital

Reputation: 12266

Works for me: using the IE8 developer tools, I can see the data attribute. Here's a screenshot.

(I know I shouldn't have to say it, but: you need to make sure that you're allowing scripts to run.)

Upvotes: 3

user1299518
user1299518

Reputation:

as you see here, data (dataset) is not supported by IE.
What you can do is rename data to data-foo and then $(..).data("foo") will work even in IE because of a special handling by jquery itself.
This is a way to bypass dataset limitation for IE.

Upvotes: 1

Related Questions