Tuesday, June 10, 2008

The saga with CallServer

I have a long story to tell here.
It all started with me having to do some process when users close the browser.
So, I need a javascript to call server, we’ve a very easy way of doing this.
Follow the traditional way http://msdn.microsoft.com/en-us/library/ms178208.aspx
The below is the piece of code for implementing CallServer in Javascript:
******************************************************************

.
.
.
body onunload="DoUnload()"
******************************************************************
I tested it and it worked fine and I pushed the code in. After some time testers found a bug. The bug was when they closed the browser the required functionality was not getting implemented. In other words the server is not getting called when browser closes. Pls note that the above javasript is in master page.
So everything worked fine in my laptop but its not working in the server for some pages.
After a lot of debugging I found that it works if I’ve an alert message after callServer in servers.
But I’ve no idea why!!!!
After a lot of soul searching in the net I found that there is an issue with AJAX enabled page and the only way out is to open up a new web page using showModalWindow and have a page load event in the html page which should close in some secs!!
I showed this to the lead developer and he said that instead of opening up and showing a new web page, why can’t I minimize it.
So this is the code for mimimzing the web page and then closing it:
******************************************************************

function onLoad()
{
moveBy(2000,2000);
setTimeout('this.close()',0)
}
body onload="onLoad()"


******************************************************************
But there was another issue. The issue was showModalWindow opens the page as a Modal Window and we cannot resize the html page. So I changed from showModalWindow to window.open.
I implemented that but there was a big issue!!
The issue with directory structure!!!
The master page is in root directory and I created the close.html in the root directory, but we’ve a lot more pages in different folders and when you are in one of the pages in sub-folder and close the browser I get an error message. Why?, b’coz the html file is not there…. It is only present in the root directory.

So I wrote a code in javascript that solved the issue which I will share momentarily, but before that I had another issue!!!

The root paths are different for different server.
Its not a cool http://webserver/rootpath
It could be http://web/dir1/dir2/rootpath


I have the rootpath in web.config and I grabbed in Global.asax Application_Start event. Then I used it in the javascript.
Here is the Application_Start in Global.asax.cs
******************************************************************
protected void Application_Start(object sender, EventArgs e)
{
Application["RootPath"] = ConfigurationManager.AppSettings["RootPath"].ToString();
}

And here is the javascript:
**************************************************************

//***This function is called when the browser window is Closed
function DoUnload()
{
if (window.event.clientX < 0 && window.event.clientY < 0)
{
CallServer("2","unlock");
var url = window.location.href;
var rootpath = '<%=Application["RootPath"] %>';
var index = url.toUpperCase().indexOf(rootpath.toUpperCase(),0);
if(index == -1)
{
//For Local codebase
window.open("http://"+window.location.host+"/close.htm");
}
else
{
//For servers
index = index + rootpath.length;
//window.showModalDialog(url.substring(0,index)+"/close.htm");
window.open(url.substring(0,index)+"/close.htm");
}
}
}
******************************************************************


So I took a deep breadth and tested and found another bug
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

After doing a hell lot of research I found that the way to solve it is remove the javascript from the "head" and place it in the "body"

So I hope this ends the Saga.

No comments: