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 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    81
    Thanks
    0
    Thanked 8 Times in 8 Posts

    rule based response for location.search

    This is a simple (but complete) example of using a rules based structure to repond to url arguments.

    Each rule is sepcifed by a name, and its action or behavior is specifed as its value.

    The example sets the bodies background image and styles textareas.
    It will repond to url arguments like: ....html?b=3&f=Arial&fs=16px

    The arguments can be in any order.
    This structure will only responde to arguments listed in the rules object.

    One small optimization is used in the function to retrieve textareas only once.

    I guess I need to note: if run,
    1. this example should be run after the textareas are available. (hint: put into a script block just bfore the close body tag.)
    2. background images are assumed to be located in a sub-directory named wallpaper
    3. the background images are assumed to be jpegs with numerical names (1.jpg, 2.jpg,...)

    Code:
    (function(){
    	var rules={
    		"b":function(n){
    			document.body.style.background="url(./wallpaper/"+n+".jpg)";
    		},
    		"f":function(n){
    				rules.tb=rules.tb||document.getElementsByTagName("textarea");
    				for(var i=0;i<rules.tb.length;i++){
    					rules.tb[i].style.fontFamily=n;
    				}
    		},
    		"fs":function(n){
    				rules.tb=rules.tb||document.getElementsByTagName("textarea");
    				for(var i=0;i<rules.tb.length;i++){
    					rules.tb[i].style.fontSize=n;
    				}
    		}
    	};
    	if(location.search.length>0){
    		var v = location.search.replace(/\?/,"").split(/&/g);
    		for(var i=0;i<v.length;i++){
    			var w = v[i].split("=");
    			try{
    				rules[w[0]](w[1]);
    			}catch(e){}
    		}
    	};
    })();
    works with ie,ff,chrome,safari,opera
    Last edited by rdspoons; 02-18-2012 at 03:44 AM. Reason: corrected missing rules object in "fs" rule

  • #2
    New Coder
    Join Date
    Jun 2009
    Posts
    81
    Thanks
    0
    Thanked 8 Times in 8 Posts
    Here a two more ways to do the same thing:

    1) Supply a rule set as an argument

    Code:
    (function(){
        var rules=arguments[0];
        if(location.search.length>0){
            var v = location.search.replace(/\?/,"").split(/&/g);
            for(var i=0;i<v.length;i++){
                var w = v[i].split("=");
                try{
                    rules[w[0]](w[1]);
                }catch(e){}
            }
        };
    })({
        "b":function(n){
            document.body.style.background="url(./wallpaper/"+n+".jpg)";
        },
        "f":function(n){
            this.tb=this.tb||document.getElementsByTagName("textarea");
            for(var i=0;i<this.tb.length;i++){
                this.tb[i].style.fontFamily=n;
            }
        },
        "fs":function(n){
            this.tb=this.tb||document.getElementsByTagName("textarea");
            for(var i=0;i<this.tb.length;i++){
                this.tb[i].style.fontSize=n;
            }
        }
        //add rules here.
    });

    2) Create a temporary objecet, supply it a rule set, run the code.

    Code:
    var url_rules1=url_rules1||(function(){
        var rules={};
        function ruleset(){
                rules=arguments[0];
        }
        function obey(){
            if(location.search.length>0){
                var v = location.search.replace(/\?/,"").split(/&/g);
                for(var i=0;i<v.length;i++){
                    var w = v[i].split("=");
                    try{
                        rules[w[0]](w[1]);
                    }catch(e){}
                }
            }
        };
        return {
            ruleset:function(){
                ruleset(arguments[0]);
            },
            obey:function(){
                obey();
            }
        }
    })();
    
    url_rules1.ruleset({
        "b":function(n){
            document.body.style.background="url(./wallpaper/"+n+".jpg)";
        },
        "f":function(n){
            this.tb=rules.tb||document.getElementsByTagName("textarea");
            for(var i=0;i<this.tb.length;i++){
                this.tb[i].style.fontFamily=n;
            }
        },
        "fs":function(n){
            this.tb=this.tb||document.getElementsByTagName("textarea");
            for(var i=0;i<this.tb.length;i++){
                this.tb[i].style.fontSize=n;
            }
        }
    });
    url_rules1.obey();
    Last edited by rdspoons; 02-18-2012 at 04:09 AM.


  •  

    Tags for this Thread

    Posting Permissions

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