简体   繁体   中英

Dynamic css on a parent window in Internet Explorer

I like to add a dynamic css file to a Parent window. I build this code for it:


function LoadJSCSSFile(filePath,fileType,parentBOO){
    //-
    var fileRef;            // Get the file reference
    //-
    //Set external JavaScript/CSS file
    switch(fileType){
        case "js":
            fileRef = document.createElement('script');
            fileRef.setAttribute("type","text/javascript");
            fileRef.setAttribute("src", filePath);
            break;
        case "css":
            fileRef = document.createElement("link");
            fileRef.setAttribute("rel", "stylesheet");
            fileRef.setAttribute("type", "text/css");
            fileRef.setAttribute("href", filePath);
            break;
        default:
            return;
            break;
    }

    //Load the file
    if(parentBOO){
        parent.document.getElementsByTagName("head")[0].appendChild(fileRef);
    }else{
        document.getElementsByTagName("head")[0].appendChild(fileRef);
    }
}

Its working fine in FireFox and Chrome but in Internet Explorer 6,7 (i do not check in 8,9 yet) i am getting the next javascript error: "Invalid argument" On this line: parent.document.getElementsByTagName("head")[0].appendChild(fileRef);

I was wondering that it is because the cross domain security so i add this line in the parent window: "document.domain = '127.0.0.1';"

But its not help.

I found the solution. The problem was that i create the element not on the same window object. To fix it i add the line: parent.document.CreateElement instead of: document.CreateElement

The fixed code is:


function LoadJSCSSFile(filePath,fileType,parentBOO){
    //-
    var fileRef;            // Get the file reference
    //-
    //Set external JavaScript/CSS file
    switch(fileType){
        case "js":
            if(parentBOO){
                fileRef = parent.document.createElement('script');
            }else{
                fileRef = document.createElement('script');
            }
            fileRef.setAttribute("type","text/javascript");
            fileRef.setAttribute("src", filePath);
            break;
        case "css":
            if(parentBOO){
                fileRef = parent.document.createElement("link");
            }else{
                fileRef = document.createElement("link");
            }
            fileRef.setAttribute("rel", "stylesheet");
            fileRef.setAttribute("type", "text/css");
            fileRef.setAttribute("href", filePath);
            break;
        default:
            return;
            break;
    }

    //Load the file
    if(parentBOO){
        parent.document.getElementsByTagName("head")[0].appendChild(fileRef);
    }else{
        document.getElementsByTagName("head")[0].appendChild(fileRef);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM