Nice challenge @shaiton, this is what I was able to reproduce in a website of mine, please take a look:
http://fedora17counter.avezzanolug.org/
I basically took the code here http://fedoraproject.org/static/js/release-counter-ext.js and customized it to be able to parse an additional (width
) parameter in the URL and get back the fedora 17 dynamic countdown logo with the given width
.
To get the new script working with both lang
and width
parameters I replaced the following portion:
var lang_match = script.src.match(/release-counter-ext\.js\?lang=(.*)$/);
if (lang_match) {
for (var i = 0; i < available_langs.length; ++i) {
if (available_langs[i] == lang_match[1]) {
lang = lang_match[1];
break;
}
}
}
with:
function getParam ( sname )
{
// url uses ? at the beginning of parameters list
var params = script.src.substr(script.src.indexOf("?")+1);
var sval = "";
// url uses & to split parameters
params = params.split("&");
for (var i=0; i<params.length; i++)
{
temp = params[i].split("=");
if ( [temp[0]] == sname ) { sval = temp[1]; }
}
return sval;
}
var lang = getParam("lang");
var width = getParam("width");
and I added this line where the other bannerimg
attributes are also set:
bannerimg.setAttribute("width", width);
I am currently using my code in avezzanolug.org website with a custom lang=it
and width=170
that perfectly fit my upper right block layout!
Note: The getParam
function assumes there are no other ? or & in the url. Of course there is a lot of room to introduce robustness in the function code (if you like @shaiton) but for my purpose it works quite well!
Reference: Inspired by http://ziemecki.net/content/javascript-parsing-url-parameters