Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-05-2013, 01:20 PM   PM User | #1
hgs
New Coder

 
Join Date: Jan 2010
Location: Germany
Posts: 52
Thanks: 1
Thanked 2 Times in 2 Posts
hgs is on a distinguished road
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
File Type: zip pframeajax.zip (4.5 KB, 35 views)
hgs is offline   Reply With Quote
Old 01-13-2013, 09:07 AM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
why does this need a php component?
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 01-13-2013, 09:48 AM   PM User | #3
hgs
New Coder

 
Join Date: Jan 2010
Location: Germany
Posts: 52
Thanks: 1
Thanked 2 Times in 2 Posts
hgs is on a distinguished road
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.
hgs is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:59 AM.


Advertisement
Log in to turn off these ads.