Saturday, 21 May 2011
Parsing the query string in Javascript into a single GLOBAL object
Two useful ideas combined:
1. From the book "Javascript the good bits", comes the idea to put all your (Javascript) global variables as ?? of a single global object called GLOBAL like this
var GLOBAL={}
GLOBAL.v1="value1";
GLOBAL.v2=0xFF;
2. If the html document is called with a URL with a search string (file.htm?v1=value1&v2=0xFF) then using Javascript you can parse the query string in the following way...
// http://stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/
// load GLOBAL with properties from the querystring at the end of the URL
// e.g. "?session=2010_2011&mod_id=COA122" produces GLOBAL.session='2010_2011' & GLOBAL.mod_id='COA122'
var GLOBAL={}
location.search.replace(
new RegExp("([^?=&]+)(=([^&]*))?", "g"),
function($0, $1, $2, $3) {
var e="GLOBAL."+$1+" = '"+$3+"'";
//alert(e);
eval(e);
}
);
Thursday, 12 May 2011
Removing rowspan from a table
I had written a data mining (scraping) script to obtain data from a table and it was working - until the table started having rowspan entries in it. I decided the easiest way to continue was to write a script to replace the rowspan entries with the requisite number of simple entries.
Here is the rowspan replacement script I used (it is written in Javascript and uses jQuery)
The reverse() bit is needed to preserve order - the script uses prepend to insert new elements so we have to start with the last element that needs work and finish with the first.
Here is the rowspan replacement script I used (it is written in Javascript and uses jQuery)
The reverse() bit is needed to preserve order - the script uses prepend to insert new elements so we have to start with the last element that needs work and finish with the first.
function replace_rowspan(section){
$.fn.reverse = [].reverse; //http://forum.jquery.com/topic/jquery-reverse-a-collection-of-jquery-elements
//script to process an array containing rowspan and re-write it without rowspan by putting in all the repeats
while($(section).find("td[rowspan]").length>0){ //are there any rowspans left
$(section).find("tr>td[rowspan]:first").each(function(i,tdi){ //find a row with a rowspan
var the_row=$(tdi).parents("tr");
$(the_row).find("td[rowspan]").reverse().each(function(j,tdj){
var c=$(tdj).clone();
var rs=$(tdj).attr("rowspan")-1;
var v=$(tdj).text();
$(tdj).removeAttr("rowspan");
if(rs>=2) //if the rowspan left is 2 or more, then leave the rowspan attribute in and let it be processed next time round
$(the_row).next().prepend($(c).attr("rowspan",rs).addClass("green") );
else if(rs==1) // otherwise this will be the last new entry and the rowspan attribute can be removed
$(the_row).next().prepend($(c).removeAttr("rowspan").addClass("red") );
})
})
}
}
Tuesday, 12 April 2011
Google Docs
iGoogle is http://www.google.com/ig
but the Lboro special one is http://partnerpage.google.com/lboro.ac.uk
If you want to write Gadgets you should add to iGoogle the following 2 gadgets: Google Gadget Editor (GGE) and the Gadget Checker
To use a DIY gadget on your own iGoogle, select publish on the GGE and IGNORE THE DIRE VALIDATION WARNINGS OF THINGS THAT NEED TO BE FIXED, then choose "Add to my iGoogle page"
If you want to have a Google Doc on view at iGoogle, use the gadget "Fetch the content of a Google document". Under "edit settings", make sure the URL you put is the "uploaded" URL of the document, not the editing URL - see Share|"Publish to the web..." from the document editor.
I have had trouble with Safari reporting Script TimeOut which may be related to multiple sign-in which I had turned on temporarily [Slow Script - Safari is no longer responding because of a script on the webpage “iGoogle” (http://partnerpage.google.com/lboro.ac.uk). Do you want to stop running the script, or let it continue?]
I get the same trouble with Firefox reporting "A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete. Script: https://docs.google.com/static/document/client/js/1254644934-kix_main_i18n_kix_core.js:115"
"User Preferences" and writing all the HTML content dynamically is dealt with at http://code.google.com/apis/gadgets/docs/basic.html.
"Saving state" is dealt with at http://code.google.com/apis/gadgets/docs/fundamentals.html.
Note especially the bit about saving into a json string.
When working with UserPrefs I thought that gadjets.json.parse(string) was not working - but I read somewhere that you have to put gadgets.json.parse(gadgets.util.unescapeString(string))
but the Lboro special one is http://partnerpage.google.com/lboro.ac.uk
If you want to write Gadgets you should add to iGoogle the following 2 gadgets: Google Gadget Editor (GGE) and the Gadget Checker
To use a DIY gadget on your own iGoogle, select publish on the GGE and IGNORE THE DIRE VALIDATION WARNINGS OF THINGS THAT NEED TO BE FIXED, then choose "Add to my iGoogle page"
If you want to have a Google Doc on view at iGoogle, use the gadget "Fetch the content of a Google document". Under "edit settings", make sure the URL you put is the "uploaded" URL of the document, not the editing URL - see Share|"Publish to the web..." from the document editor.
I have had trouble with Safari reporting Script TimeOut which may be related to multiple sign-in which I had turned on temporarily [Slow Script - Safari is no longer responding because of a script on the webpage “iGoogle” (http://partnerpage.google.com/lboro.ac.uk). Do you want to stop running the script, or let it continue?]
I get the same trouble with Firefox reporting "A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete. Script: https://docs.google.com/static/document/client/js/1254644934-kix_main_i18n_kix_core.js:115"
"User Preferences" and writing all the HTML content dynamically is dealt with at http://code.google.com/apis/gadgets/docs/basic.html.
"Saving state" is dealt with at http://code.google.com/apis/gadgets/docs/fundamentals.html.
Note especially the bit about saving into a json string.
When working with UserPrefs I thought that gadjets.json.parse(string) was not working - but I read somewhere that you have to put gadgets.json.parse(gadgets.util.unescapeString(string))
Subscribe to:
Comments (Atom)