Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    hgs
    hgs is offline
    New Coder
    Join Date
    Jan 2010
    Location
    Germany
    Posts
    80
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Script to load and display external content

    Hi

    Here is another script to request and load content from a server.
    In addition to this the script scans for embeded css and script tags and acts accordingly.

    In the exampel http://hgsweb.de/pframe this is done for Menu1

    Code:
    'use strict';
    var HGSPFRAME = {
        toggle: false,
        xmlHttp: function() {
            if (window.XMLHttpRequest) {
                return new XMLHttpRequest();
            } else {
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
            alert("Error creating the XMLHttpRequest object.");
            return '';
        }(),
        async: false, // we will wait for the backend to complete
        fillPframe: function(pfid, backend_script) {
            ////////////////
            // pfid is the id of an html elemnt to fill with content
            // delivered by the backend_script
            // If this html element does yet not exist we create a div
            // to hold the content. 
            ////////////////
            var dd = document.getElementById(pfid);
            if (!dd) {
                dd = document.createElement('DIV');
                dd.id = pfid;
                document.body.appendChild(dd);
            }
            dd.innerHTML = "...";
            var xmlHttp = HGSPFRAME.xmlHttp;
            xmlHttp.open("POST", "../pframe/pframeajax.php", HGSPFRAME.async);
            xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlHttp.onreadystatechange = function() {
                if (this.readyState === 4 && this.status === 200) {
                    var  i, dd, result,ls,nl;           
                    var jresp=eval('('+xmlHttp.responseText+')');
                    //////////////////
                    // result is encoded base64 on the server side
                    // this way we can send/receive any data over the wire 
                    //////////////////
                    result = decode64(jresp.result);                
                    dd = document.getElementById(jresp.pfid);
                    if (HGSPFRAME.toggle) {
                        if (dd.style.display !== "none" && dd.style.display !== "") {
                            dd.style.display = "none";
                        } else {
                            dd.innerHTML = result;
                            dd.style.display = "block";
                        }
                    } else {
                        dd.innerHTML = result;
                        dd.style.display = "block";
                    }
                    ///////////////////
                    // here we look for CSS style sheet via link element
                    ///////////////////
                    ls = dd.getElementsByTagName('LINK');
                    nl=ls.length;
                    for (i = 0; i < nl; i++) {
                        if (ls[i].type==='text/css' && ls[i].rel==='stylesheet') {
                            HGSPFRAME.includeCSS(ls[i].href); 
                        }
                    }
                    ///////////////////
                    // here we look for and execte/load any given JavaScript              
                    ///////////////////
                    ls = dd.getElementsByTagName('script');
                    nl=ls.length;
                    for (i = 0; i < nl; i++) {
                        if (ls[i].src==='undefined' || ls[i].src==='') {
                            eval(ls[i].innerHTML); // executes immediatly !!!
                        } else {
                            HGSPFRAME.includeJS(ls[i].src); // this is evaluated later
                        }
                    }
                }
            };
            xmlHttp.send("pfid=" + pfid + "&script=" + encodeURIComponent(backend_script));
    
            //  Base64 encode / decode
            // http://www.webtoolkit.info/
    
            function decode64(input) {
                var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
                var output = "";
                var enc1, enc2, enc3, enc4;
                var i = 0, l = input.length;
                input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
                while (i < l) {
                    enc1 = keyStr.indexOf(input.charAt(i++));
                    enc2 = keyStr.indexOf(input.charAt(i++));
                    enc3 = keyStr.indexOf(input.charAt(i++));
                    enc4 = keyStr.indexOf(input.charAt(i++));
                    output = output + String.fromCharCode((enc1 << 2) | (enc2 >> 4));
                    if (enc3 !== 64) {
                        output = output + String.fromCharCode(((enc2 & 15) << 4) | (enc3 >> 2));
                    }
                    if (enc4 !== 64) {
                        output = output + String.fromCharCode(((enc3 & 3) << 6) | enc4);
                    }
                }
                output=utf8_decode(output); 
                return output;
            
                function utf8_decode(utftext) {
                    var string = "";
                    var i = 0;
                    var  c,c3,c2;
                    c = c2 = 0;
                    while ( i < utftext.length ) {
                        c = utftext.charCodeAt(i);
                        if (c < 128) {
                            string += String.fromCharCode(c);
                            i++;
                        }
                        else if((c > 191) && (c < 224)) {
                            c2 = utftext.charCodeAt(i+1);
                            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                            i += 2;
                        }
                        else {
                            c2 = utftext.charCodeAt(i+1);
                            c3 = utftext.charCodeAt(i+2);
                            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                            i += 3;
                        }
                    }
                    return string;
                }
            }
        },
        includeJS: function(file) {
            var l=document.getElementsByTagName('script');
            var i;
            for(i=0;i<l.length;i++){
                if(l[i].src.indexOf(file)>0){
                    return;
                }
            }        
            var script = document.createElement("script");
            script.setAttribute("type", "text/javascript");
            script.setAttribute("src", file);
            var heads = document.getElementsByTagName("head");
            for(var i=0;i<heads.length;i++) {
                heads[i].appendChild(script);		
            }
        },
        includeCSS: function(path) {      
            var l=document.getElementsByTagName('LINK');
            var i;
            for(i=0;i<l.length;i++){
                if(l[i].href.indexOf(path)>0){
                    return;
                }
            }  
            var link = document.createElement("link");
            link.setAttribute( "rel", "stylesheet");
            link.setAttribute( "type", "text/css");
            link.setAttribute( "href", path);
            var heads = document.getElementsByTagName("head");
            for(var i=0;i<heads.length;i++) {
                heads[i].appendChild(link);		
            }
        }
    };
    Regards
    Heinz
    Attached Files Attached Files

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,186
    Thanks
    10
    Thanked 569 Times in 550 Posts
    why does this need a php component?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #3
    hgs
    hgs is offline
    New Coder
    Join Date
    Jan 2010
    Location
    Germany
    Posts
    80
    Thanks
    3
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by rnd me View Post
    why does this need a php component?
    It does not need a php component.

    It should work with any backend process/script , however I never used anything other then PHP.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •