Skip to content Skip to sidebar Skip to footer

Append Element In Head Of An Iframe Using Jquery

i want to append a style sheet(css) link to the head of an iframe using jquery . i tried with the following code but not working. $('#tabsFrame').contents().find('head').append(css

Solution 1:

i am used to append data to an iframe by using this line of code

$('body', window.frames[target].document).append(data);

In your case, this line would look like this

$('head', window.frames['tabsFrame'].document).append(cssLink);

EDIT:

Add <head></head> to the iframe and change your var cssLink to

cssLink = '<link href="cupertino_1.4/css/cupertino/jquery-ui-1.8.7.custom.css" type="text/css" rel="Stylesheet" class="ui-theme" />

Solution 2:

well, you can check with this:

$('#tabsFrame').contents().find("head")[0].appendChild(cssLink);

Solution 3:

I believe you can't manipulate the content of an iframe because of security. Having you be able to do such a thing would make cross-site-scripting too easy.

The iframe is totally seperate from the DOM of your page.

Also, java and javascript are two completely different things!

Follow the Link to see the difference here

Solution 4:

This could be related to IE not allowing you to add elements in the DOM, check out the clever solution here

EDIT:

Thanks @kris, good advice to add more info in case links break:

Here is the main code snippet from the link, in case it goes out again. (This is only needed with some IE version, for the most part, the other answer work just fine)

var ifrm;

//attempts to retrieve the IFrame document    functionaddElementToFrame(newStyle) {
    if (typeof ifrm == "undefined") {
        ifrm = document.getElementById('previewFrame');
        if (ifrm.contentWindow) {
            ifrm = ifrm.contentWindow;
        } else {
            if (ifrm.contentDocument.document) {
                ifrm = ifrm.contentDocument.document;
            } else {
                ifrm = ifrm.contentDocument;
            }
        }
    }

    //Now that we have the document, look for an existing style tagvar tag = ifrm.document.getElementById("tempTag");

    //if you need to replace the existing tag, we first need to remove itif (typeof tag != "undefined" || tag != null) {
        $("#tempTag", ifrm.document).remove();
    }

    //add a new style tag
    $("HEAD", ifrm.document).append("");
}

Post a Comment for "Append Element In Head Of An Iframe Using Jquery"