Saturday, February 20, 2010

I have got thru the next step. I was able to communicate with the server and use timers to keep trying until the full script was loaded. So let me step thru where I am at.

First is a simple Initialization routine.


function Initialize() {
TalkToServer();
}



The TalkToServer function is just a placeholder to test talking to the server. I had formally used self and I ran into problems with IE so I changed it to myself. Clever, eh? This I am treating as an object rather than just a function. There are four variables; myself, myRequest, scriptsloaded and t another object. There are two methods login and verify. The basic outline is to verify that scripts are loaded and then to perform a login attempt which is just a way to test communication with the server. The username and password are hardcoded because I am just testing communication.



function TalkToServer() {
myself = this;
var myRequest;
var scriptsloaded = false;
var t = new myTimer();
this.login = function() {
myRequest = new ajaxObject('http://kurtclement.com/login.php',processData);
myRequest.update('password=mypassword&username=myusername','POST');
}
this.verify = function() {
if(scriptsloaded) {
myself.login();
} else {
scriptsloaded = VerifyScriptLoaded('tools/tools.js');
t.tryagain(myself.verify,100);
}
}
this.verify();
}




The timer object takes a function and calls that function after the timeout period.


function myTimer() {
var myself = this;
var mymax = 5;
var mycount = 0;
this.tryagain = function(f,t) {
if((mycount++)<mymax) {
setTimeout(function(){myself.what(f);},t);
} else {
alert('max '+mycount);
}
};
myself.what = function(f) {f();};
}

I spent a lot of time trying to dynamically load scripts. The first problem is that you can't run functions until the scripts are loaded. That is pretty easy to solve. I built my own little framework for testing whether the script was loaded. Then I ran into another problem. If the script wasn't loaded I needed to set a timer and try again later. Well the timer in JavaScript is a little funky in that you can't pass parameters to it easily. After much trial and tribulation (and reading) I came up with this:



function mytimeout(w,t) {
var self = this;
self.what = function(c){alert(w);};
self.mytimer = function() {setTimeout(function(){self.what(w);},t);};

}
function Initialize() {
//Load Scripts

alert('loading');
var t1 = new mytimeout('bob',3000);
var t2 = new mytimeout('carol',1000);
t1.mytimer();
t2.mytimer();

}



This works nicely on Firefox for Max and IE for Windows. This of course is the generic version. The Initialize gets called in the index.html and this is in a loaded JavaScript file. I hope someone else might find this useful. I think it is pretty easy to understand. To give fair credit I did work off the reading from the website:

http://www.west-wind.com/weblog/posts/5033.aspx

Friday, February 12, 2010

I finally figured out an easy way to post code samples. I spent way too much time trying to learn the intriquisies.

I finally figured out an easy way to post code samples. I spent way too much time trying to learn the intricacies of blogspot. This site: http://francois.schnell.free.fr/tools/BloggerPaste/BloggerPaste.html provides a simple way to convert.

So what I was trying to post before I fell down this rabbit hole was that I kept running into errors in IE that I was not seeing in Firefox. It was minor, but I really don’t want to have these errors. I know I see errors on Yahoo and other mainstream sites that get suppressed if you do not have error checking enabled, but I just didn’t want them. What I found was on this site: http://blog.roberthahn.ca/articles/2007/02/02/how-to-use-window-onload-the-right-way/

I won’t go into the details of why it works and why it necessary, but the final result was the way that I loaded the initial javascript code

<script>
function makeDoubleDelegate(function1, function2) {
return function() {
if (function1) function1();
if (function2) function2();
}
}
</script></head><body><script>
window.onload = makeDoubleDelegate(window.onload, Initialize );
</script>


This eliminated the errors, now back to working on a framework.

Thursday, February 11, 2010

Deep into the weeds

I was just going to update the little I had done when I got caught up in the details of blogging. I first just posted some code sample and immediatly found out that code gets interpreted in the HTML. So a quick search had some sample of how to post code samples, but then I got caught up in the templates and everything else and I loss track of the original goal which was to just update the blog. What a waste.