This one could save you lot of time :-) In short, the answer is to “return false;”. That’s it. If you want to know more details, read ahead.

Say, you have an awesome page where you want to disable right click and also disable Ctrl+C (copy), Ctrl+S (save page). The process is simple. Just register an event for right click and when the event fires, just return false. Since false is being returned, the browser will cancel the event.

Same goes with keyboard. ‘onkeypress’ event is fired whenever a key is pressed. To reject any key, just capture that event and return false. If you want to do anything special, just do it before returning false. Ctrl+S is interesting. In most web browsers (notably google chrome), it brings up the default Save Dialog. Most web apps have keyboard shortcuts these days & oh! 99% of them don’t return false, only to find the user experience irritating when the save dialog pops up; when something else should occur. Hence, if you wish to customize the functionality of the default browser specific keyboard shortcuts, do the following:

  1. Create an event handler and capture the event.
  2. Do something.
  3. “return false;”

That’s all. Have fun!

Here’s the trick below to open multiple gmail accounts with the same version of firefox.

  1. Open the Run dialog. In linux, press Alt+F2. On Windows, press WinKey+R.
  2. In the dialog box, type “firefox -p -no-remote”. This will bring up firefox’s profiles dialog box.
  3. Click on “Create Profile” and follow the wizard. Lets assume you named the profile as “gmail2“.
  4. Now close the dialogs and return to desktop.

Now, open the Run dialog again. In the dialog box, type “firefox -p gmail2 -no-remote“.

“-no-remote” option is very important because it creates an isolated session that is different from the currently open firefox windows of other profiles. If -no-remote option is not given, then a new window of the currently running profile or the default profile is opened, where session sharing happens (which we don’t want).

Now, since sessions aren’t shared between different profiles, you can open two accounts and happily have them.

Suggested Improvement for Firefox:

Internet Explorer 8 has this amazing option in the File menu where you can open a completely new session of IE. Sessions are completly isolated and gives lot of relief. Its available at File->New Session. Hope this feature is brought into Firefox as well. Or if this feature is already available as a plugin, please let me know in the comments. Actually speaking, this feature was available in IE since IE 6 (AFAIK). In IE6/7, you can open a completely new session by clicking the IE icon on the desktop.

There are a number of use-cases for which you would want to browse a mobile-optimized website on your PC. When you visit the mobile website on your PC’s web browser, the website displays the full content much to your dismay. However, when you visit the browser on a mobile, it displays a perfectly mobile optimized page.

In these cases, there are some simple steps that you can follow to open mobile websites on your PC:

  1. Download and install the latest version Firefox from http://www.getfirefox.com/.
  2. Visit Firefox Add-ons page and download the Modify Headers addon.
  3. Install the addon and restart firefox.
  4. From the firefox window, select “Tools” menu & click on “Modify Headers” option (Tools -> Modify Headers).
  5. The window will open as shown:
    Modify Headers Addon Window Click to enlarge
  6. Below the title bar, there’s a drop down. Select “Add” from the drop down box.
  7. Now in the text box next to the drop down, type “user-agent”.
  8. In the third text box, paste this string – Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaE51-1/100.34.20; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413.
  9. Click on “Save”.
  10. The screen should look like as shown in the screen shot below:
    Filled screen of the addon Click to enlarge
  11. Using the buttons on the Modify Headers addon window, you can enable or disable particular items.
  12. That’s it! Whenever you want to view a mobile website, just go to Tools->Modify Headers and enable the user agent you added in step 8. When you don’t need it, just open this window and disable it.
  13. You can close addon window after you have enabled/disabled items.
  14. Enjoy! :)

Behind the Scenes:

Most mobile websites make use of the HTTP header called “User-Agent”. This header tells the server what kind of web browser the user has. So, mobile websites usually look for the name of the device manufacturer in the User-Agent header. If it matches one of the manufacturers then the mobile optimized web page is sent.

That’s about it! Hope you enjoyed this brief.

Now go and figure out how to display the iPhone version of Facebook on your PC ;)

Recently I needed a way for Threaded Tweets to do HTTP basic authentication to try out some stuff. As you probably know by now, it is written in javascript and uses no server side code. So, I had to find out some way to pass basic authentication credentials through the XMLHttpRequest object so that I could be authenticated with the Twitter server.

Turns out that it is pretty simple. If you use FireBug on any website that supports Basic Authentication, you will note that a new HTTP header called ‘Authorization’ is added. It looks like:

Authorization: Basic aDkdjfZy==

Now all you need to do is somehow pass on this header to the XMLHttpRequest object. If you use the native XMLHttpRequest, you can use the setRequestHeader method to do this:

xhr.setRequestHeader(“Authorization”, “Basic aDkdjfZy”);

What if you are using jQuery? Then also its pretty simple. jQuery’s AJAX object allows us to change the XHR object before the AJAX request is sent by setting the “beforeSend” callback; which is done as follows:

$.ajax ( {
url: “http://abc.com/”,
beforeSend: function (xhr) { xhr.setRequestHeader (“Authorization”, “Basic aDkdjfZy==”); },
succes: function(val) { alert(val); } } );

Base 64 Encoding:

One important part of Basic Authentication is that, you need to encode the username and password into Base 64. There are tons of Base 64 encoding tools written on javascript, which you can use. Example below:

xhr.setRequestHeader(“Authorization”, “Basic ” + encodeBase64 (“username:password”) );

That’s about it. Hope this was useful to you !

Some Updates

Cross-domain GET requests work all the time. The problem is with POST requests. Most browsers don’t allow XHR to POST data to a domain that’s different from the one in which the page is loaded. For this to work correctly, the ‘document.domain’ property must be set correctly.

The second best way to POST to another domain is to use an iframe and submit the form with the iframe as the “target”. Again, to read the result of the iframe, the “document.domain” of the parent and that of the iframe should match. This can be easily set through javascript. Thus, when the form post is done and the page is loaded, its value can be accessed.

Note: Chrome and Safari don’t allow access to iframe’s DOM if its document.domain is different from that of the iframe’s parent.

That is a very common error that users generally encounter when packages are being compiled. Though the log files mention something bizarre, this error can be resolved quite easily by running the following command:

$ sudo apt-get install build-essential

That would do for most cases. Many programs also require a compiler-compiler called ‘bison’. You may install that also by giving,

$ sudo apt-get install bison

This would make sure “configure” scripts work properly.

I was playing around with Ubuntu & installing a few stuff from the terminal. One problem that quickly became a pain in the rear is that, I had to `sudo` everytime and give a password whenever a command that required admin privileges had to be run. Since, I was installing a few stuff, almost all commands required admin rights. So, without much ado, here’s how to open a terminal with permanent root privileges.

  1. Press Alt+F2. The “Run Application” dialog will pop up.
  2. Type “gnome-terminal” in the dialog and press “Enter”. This will open a new terminal window without admin rights.
  3. Now, in the new terminal window, type “sudo gnome-terminal”. You will be asked for your password. Give your password and press “Enter”. A separate terminal window with root privileges will open now. This is immediately visible because the usual “$” prompt changes to a “#” prompt.

There you go, :-) three cool steps to have your terminal with admin rights. If you press “Ctrl+Shift+N” from this new terminal, it will open another terminal window, which also has root privileges.