September 26th, 2007 — Code, JavaScript
Lately, I have been plagued with the complicated tasks of solving problems at work that require dual monitors. However, I only have a single monitor. Until Today!
Today, I got my PC setup with Dual Monitors and was asked to solve the following problem:
“When the user clicks on a link that opens a new window using window.open. Make the window appear on the same monitor as its’ parent.”
So I got started. Quickly I discovered that window.screenLeft (for IE) and window.screenX (for everyone else) will tell me where the window’s border is on the desktop. So what better to do than to throw that in a window.*property* which gave me this code:
// Find Left Boundry of current Window
function FindLeftWindowBoundry()
{
// In Internet Explorer window.screenLeft is the window's left boundry
if (window.screenLeft)
{
return window.screenLeft;
}
// In Firefox window.screenX is the window's left boundry
if (window.screenX)
return window.screenX;
return 0;
}
window.leftWindowBoundry = FindLeftWindowBoundry;
Next, I needed to find the monitor’s left border. Soon after, I figured out that there is no property currently for this, however, you can get the width of the current monitor your window is on by using window.screen.width. There is a big pitfall that needs to be considered, you can have a monitor whose left border starts with a negative X-axis (ie: -1024×0 to 0×768).
So that left with me the following code:
// Find Left Boundry of the Screen/Monitor
function FindLeftScreenBoundry()
{
// Check if the window is off the primary monitor in a positive axis
// X,Y X,Y S = Screen, W = Window
// 0,0 ---------- 1280,0 ----------
// | | | --- |
// | | | | W | |
// | S | | --- S |
// ---------- ----------
if (window.leftWindowBoundry() > window.screen.width)
{
return window.leftWindowBoundry() - (window.leftWindowBoundry() - window.screen.width);
}
// Check if the window is off the primary monitor in a negative axis
// X,Y X,Y S = Screen, W = Window
// 0,0 ---------- -1280,0 ----------
// | | | --- |
// | | | | W | |
// | S | | --- S |
// ---------- ----------
// This only works in Firefox at the moment due to a bug in Internet Explorer opening new windows into a negative axis
// However, you can move opened windows into a negative axis as a workaround
if (window.leftWindowBoundry() < 0 && window.leftWindowBoundry() > (window.screen.width * -1))
{
return (window.screen.width * -1);
}
// If neither of the above, the monitor is on the primary monitor whose's screen X should be 0
return 0;
}
window.leftScreenBoundry = FindLeftScreenBoundry;
Now that the code is written, you can now use window.open to open a window on the monitor the parent window is on.
window.open(thePage, 'windowName', 'resizable=1, scrollbars=1, fullscreen=0, height=200, width=650, screenX=' + window.leftScreenBoundry() + ' , left=' + window.leftScreenBoundry() + ', toolbar=0, menubar=0, status=1');
Oh yeah, IE has a bug in it that prevents opening windows with window.open in the negative X-axis. You have to use windowName.moveTo(-1024, 0); to move it to the negative X-axis.
Enjoy!
September 25th, 2007 — .NET, Ajax, Code, PHP, Work
There has been much debate on using XML or JSON with your AJAX, but in my mind, the debate is pointless. Both are nice, one is quicker, one is slower. However, when it comes to development pick what you know best and what you can implement/maintain easiest.
If you do this, you are guaranteed the following:
- It is easy to maintain and understand the code you wrote months later
- Chances are your application will not be the quickest in response
- You will make use of techniques you fully understand than trying to adapt ones that you are unfamiliar with and may implement wrong
So how does JSON differ from returning XML in your AJAX requests? Lets first look at the advantages of XML.
- Standardized and easily serialized into via .NET
- The XML is easy to read all within itself
- You can make use of XSLT, DTDs, etc
How about disadvantages of XML?
- It is slow in comparison to JSON
- It can be clumsy when not designed properly
Now for the advantages of JSON:
- It is faster than XML to parse in JavaScript
- There are addin’s you can install in .NET or a library in PHP to serialize a JSON string from an object
- It is hard to make this clumsy as it is based on the Object Design
And the disadvantages of JSON:
- It is harder to read the serialized output
- You can’t use XSLT, DTDs, or something of the sort to validate or style the request
As for myself, I am a JSON person. I didn’t used to be though. My original AJAX applications were focused around XML primarily because I could make use of an XSLT and I always validated against a schema. However, JSON has won me over due to speed. JSON’s speed is just ridiculously fast and it showed when I converted a major application over to it. I saved nearly a third of the time it took to do a fairly simple task.
However, pick the poison that you understand most, otherwise, it could have detrimental affects.
September 16th, 2007 — .NET, Ajax, Code, JavaScript, PHP, Projects, Work, kiosk
This weekend has been a nice relaxing weekend for me and so when I got back home this afternoon (on Sunday the 16th), I was in a great mood to refactor some of my kiosk code base. Now, I should start by saying, I decided to refactor code instead of write new code tonight because I wanted to lessen the number of files that needed to load in the Ajax functionality.
Kiosk, uses Ajax in interesting ways to provide a better experience, but with that, the Ajax applications were built as each page was assembled. This caused functionality across pages to get a bit messy or functionality shared across pages to get thrown into individual files. Well the problem with individual files, is that is yet another file for the browser to download.
So before I get any further, let me discuss refactoring and how it can solve many performance binds your application may be experiencing.
To start off, refactoring is taking common code, or code that is used across pages or frequently and moving it to a more global location so it can be called easier and the code only has to be written once.
So where can this help?
- First of all, by refactoring code into a single global file, you can limit the number of HTTP requests if that file is accessible via the web. Since your common code is in a single file, the browser only has to fetch that code once, and will then likely cache that file for each page after, giving you even better performance.
- Secondly, by placing the common code in a global method/function, you are writing less code. As now when you need to call that code on your second page, it is already written, so just call the function. Otherwise, you would need to write the code all over again.
- Thirdly, less code equals smaller files. By refactoring your code, you can have smaller file sizes and in turn that is quicker to download, quicker to parse, and quicker to execute.
Now in my situation, I took about 4 JavaScript files and combined them into a single file adequately named “general.js”. The file contained code to automatically log the user out after a set amount of inactivity in the UI (User Interface). Secondly, it contained the version checking mechanism calls that see if they are running the latest version of the software. Unfortunately, those are the only two things it does right now, but as the project progresses, I am sure more utilities will get added in this file.
My next task is to refactor the Data Grid functionality so it is 1) XHTML, 2) easy to build complex columns with complex rows, 3) customizable by identifying StyleSheet classes for the columns and rows. Hopefully, I can get my head around this idea and get it working with little trouble to the programmer.
So what can you think of that you need to refactor? What performance gains do you suspect you will achieve with the refactoring you do? Any performance markers?