...

View Full Version : rule based response for location.search



rdspoons
02-18-2012, 01:55 AM
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,...)



(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

rdspoons
02-18-2012, 04:05 AM
Here a two more ways to do the same thing:

1) Supply a rule set as an argument



(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.



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();



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum