Showing posts with label Web. Show all posts
Showing posts with label Web. Show all posts

Friday, November 9, 2012

Closing all Child Windows using JavaScript

Hi everyone,

Sorry that it took me so much time to write this 2nd part post, I was pretty loaded at work and personal life, and frankly, I still need to get used to write blog posts ;). I hope that in the future I will be able to write more often, and without these long pauses. Anyway, let’s get down to business.

In my last post I described the way we handle window opening in one of the web application we work on. Just to remind you, we have a single main page that has to be stayed open all the time. This page opens a lot of other page as pop-ups. Those pop-ups can open their own child windows, as pop-up windows or pop-up modal dialogs. Actually it looks like this:

child-windows

I will actually go straight to the implementation of this, but first there are few things we have to remember:

  1. The main page has to be always open, and when it closes all the child windows has to be closed to. So we need to close the child windows when the parent windows are closed, (or starts the process of closing).
  2. When there is a modal dialog opened, the parent window that opened it is frozen, hence cannot run any JavaScript.
  3. The child window cannot always know about his parent window, but the parent window always knows about all of his child windows. That means that the parent should be responsible for the closure of the children.

Given all that, this is how we need to close the child windows:

closing-windows

Finally, code:

Every parent window (e.g. window that opens other windows) should hold a list of references to its child windows, and when it opens a new window it should be added to that list. So we will add this JavaScript code to the head part of the page:

<script type="text/javascript">
var childWindows = [];

function OpenChildWindow() {
var openedWindow = window.open(url, pageName, params);
childWindows[childWindows.length] = openedWindow;
}
</script>

This should be in every page that can open other pages as separate window.


But what about this modal dialog thing? Well, this can be a little bit tricky. Because the window.openModalDialog() function doesn’t return a reference of the dialog, we need to do something like this in the parent script:

<script type="text/javascript">
var childWindows = [];

function OpenModalDialog() {
window.showModalDialog(url, window, params);
}
</script>

And this script in the child dialog:

<script type="text/javascript">
window.dialogArguments.childWindows[window.dialogArguments.childWindows.length] = window;
</script>

This code will pass a reference of the parent window to the child in the second argument (which can used to send all sort of data to the modal dialog), and the modal dialog is responsible to register itself to his parents’ childWindows list, when it’s loaded.


Following this pattern in all of the windows, and you will be able to get to any window from the main page within your code. This is important because of #3 above.


Next, I added this code the main page:

<script type="text/javascript">
function CloseAllChildWindows(window) {
for (var i = 0; i < window.childWindows.length; i++) {
if ((typeof window.childWindows[i].childWindows != "unknown") &&
(typeof window.childWindows[i].childWindows != "undefined")) {
if (window.childWindows[i].childWindows.length > 0) {
CloseAllChildWindows(window.childWindows[i]);
}
}
if (!window.childWindows[i].closed) {
window.childWindows[i].close();
}
}
}
</script>

This main page function will be called when the body.onunload is fired:

<body onunload="CloseAllChildWindows()">

It will go thru all windows, recursively closing all of them, starting from the youngest up to the oldest, and finally the main page will close itself. This will take care of #1 above.


You can notice that the function always check the second hierarchy from the current window. That’s because #2 - The script can’t ask a direct child window to close it’s modal child window, because this will hand the browser process, because the JavaScript code is jammed. Remember modal dialogs freeze their parent window? :)


You can enhance this logic, and check if there are child windows opened, and if there are, prompt the user and ask him if he is sure that he wants to leave the website. Actually, I’ve done this. You can achieve by adding this script to the main page:

    function ThereAreOpenedChildWindows() {
for (var i = 0; i < window.childWindows.length; i++) {
if (!window.childWindows[i].closed) {
return true;
}
}
return false;
}

function PromtUserClosingAllWindows() {
if (ThereAreOpenedChildWindows()) {
window.event.returnValue = "Leaving this page will close all child windows. Continue?";
}
else
return;
};

Note: I tested this code on IE and Chrome. It only works on IE, but I was OK with it since this is what we needed. There is no guarantee that this code will work on different browsers.


And register to the onbeforeunload event of the body:

<body onunload="PromtUserClosingAllWindows()">

That’s it! Hope this post was helpful, good luck closing child windows :)

Sunday, July 15, 2012

Child Windows Issues

At my work we have a web application which is designed as a single main page which is sort of a “main control page”, that needs to be staying open all the time. Also, there are many other pages on the app which provides the ability to manage data, preform actions, configure settings, etc. The user needs some way to reach those pages, but since the main page has to stay open all the time, it was decided that those pages will be opened in a pop-up styled separate window. That way the users can do what they like while the main page remains opened at the background. For example:

Child windows

That’s not all.

Some of those child windows show their own pop-ups that are opened as a modal dialog (mostly to interact with the user – success/failure of operations, validation…). As you may or may not know, modal dialogs block the parent window, and only the parent window (in fact, it freezes the parent window from running JavaScript at the background). That means that the main window stays reactive all the time. Generally it looks like this: 

Child window with a modal

Also, some of those pop-up windows have links to other windows, which are opened as pop-ups of the pop-up window. Confused? Me too. The main window has a child window, which also has a child window. So actually we possibly have an unknown number of “levels” of child windows. In theory that number can get very large.

Combining all of the above, it may look like this:

Jungle

Here comes the problem. Recently we’ve noticed that the end users sometimes close the app’s main page, and forget to close a child window which accidently stayed opened. The result is an “orphan” pop-up which, of course, is useless without the main page :)

After we discovered this issue, we decided that the main page should be responsible for the closure of all its child windows when it is closed. We had to come up with a smart enough way to handle the complex cases we have.

In the next post I’ll show the implementation for this, stay tuned.

Sunday, June 3, 2012

Inviting Crawlers

Note: I will focus here on Google search engine, but most of this works for all major modern search engines.

After understanding the basics about search engines and how they work, I decided to act for improving my site search visibility – how my pages will be appeared in search results and how high they will be ranked.

So I started seeking the web for ways to do so, and apparently there are many ways you can do that. In fact, there is an entire field called Search Engine Optimization (SEO), there are even SEO Consultants that make money from this stuff. Sure I’m no SEO, but I’ve gathered some simple do-it-yourself actions that you can do to optimize your search results:

  • Explicitly tell Google to crawl a site. This is a great start for new sites.
  • Design your site to be more crawler-friendly, by structuring your URLs and using Meta tags, such as the <meta name="description" content="…" /> tag.
    Tip about meta tags: Google ignores most Meta tags. The most important one is the description tag, which besides helping crawlers to get a clue about the content of the page, it tells Google what to display under your link. A good description tag will eventually make more visitors to your site.
  • Add links to your site from other pages around the web. Especially from pages similar in content. This is extremely important. It will help your pages to be more visible on searches because crawl robots will get to your site more often and mark it as more relevant and popular.
  • Search engines rank pages by the relevance to the term being searched. For example, if you search for “Code Lodge” you can find a lot of stuff that has no connection to software, but if most of the people that searched this term will enter to the http://www.codelodge.net/ result, it will eventually result in ranking this site higher. So the more clicks you get, you will be rated higher.
  • If you own a blog, then you probably want to add credibility and authorship to your pages. By making sure each page has a link with a rel="author" attribute to your , and from your Google+ page a link that has rel="me" with a link back to your blog. This is how you make your site gain reliability, and with a cyclic link back to your site it will be crawled more often.
  • Pay a SOE guy to do this job for you. :)

Those are some great tips but the best advice that I’ve learned from this session, is simply try to build a quality site with rich content and the rest will come by itself. You can’t build a crappy site that the only good thing about it is it’s meta tags and expect good rankings. Clear your mind from thoughts about search engines and doing tricks that will help you with good search results. Start focusing on building sites with good UX, content, and design that will be good for your visitors, that’s 99% of what matters. You have to work for it, there are no free rides.

nofreerides

That’s all for now, I’m sure that there are much more things I can do, but all those kinds of things takes time to show results. I think I will let time to do its job. Until then I better start working.

Saturday, May 26, 2012

SEO 101

One of the first things I did after opening my blog was to think how people are going to reach it, so I started with searching for it in Google. I entered “The Code Lodge” hit enter and I couldn’t find my site anywhere. Hitting “Code Lodge” also got me nothing. First I thought that something must be wrong with the process of launching the site, but on second thought it might have sense.

 WildCrawl

I assumed that the reason for that was because I just launched it a few hours ago and it’s only a baby in terms of the web. It got my thinking about how these things work, and what can I do to make my site searchable. I realized that I was clueless about the process which modern search engines works on and that it’s a huge con on my list.

So how does the search engine magic work? It is comprised of two main stages: Crawling and Indexing.

The process of crawling is generally accessing public content (webpages) and following the links that are on those pages. This is done nonstop by automated bots.

The robots.txt file can help you allow or prevent access to specific content on your site to the robots that crawl your site. It should be located in your root folder and has to be called robots.txt. Make sure you don’t abuse it to secure private content. For an example of the robots file you can view http://www.facebook.com/robots.txt .

Indexing is the process of gathering information about a page so it will be available in search results.

It is important to distinguish between the two stages. Your page can be crawled but not indexed and vice versa. Intervening with the process of one of those stages can extremely affect your search visibility, so do it with caution.

As you can see, the available (public) content on your site has a major effect on the search process. By allowing the search engine to crawl a page, all its links will be also crawled, the content that your pages contain will be indexed and eventually shown in search results.

SEO? Stay tuned for how to optimize your sites search results.