February 2007

How to get the selected text on a page with Javascript

There are lots of reasons to need the currently selected text on a page in Javascript to do some manipulation. Unfortunately, there are about as many ways to get it as there are browsers.

I initially looked at the document.selection object, but it only works in Internet Explorer. After a bunch of heartache and searching, I came across the quirksmode site, which BTW, I highly recommend for anyone interested in Javascript/DOM.

The author offers a function that gives you some nice cross browser functionality. I’ve modified it to simply return the selected text as a string, instead of output it to the page.

function getSel() {
	if (window.getSelection) {
		return window.getSelection();
	} else if (document.getSelection) {
		return document.getSelection();
	} else if (document.selection) {
		return document.selection.createRange().text;
	}
	return;
}


Could it get any simpler?

Javascript

Comments (0)

Permalink

How to get the focus of an object with Javascript

I thought it would be easy, I found the hasFocus() function, but apparently it only works for the document object. I had to hack this one, so I simply set a global variable to store the focus state of the input field I wanted to monitor, and set the onfocus and onblur handlers for the input field that set the global variable true or false. It’s not clean, but it works like a charm.

In the document’s head:

<script type="text/javascript">
var boxFocusState = false;
</script>

The text box:

<input type="text" id="search" onfocus="boxFocusState=true;" onblur="boxFocusState=false;" />

That’s all it took!

Javascript

Comments (0)

Permalink

Google keeps experimenting with adsense design

Remember when Google began experimenting with “Ads by Gooooooogle” instead of “Ads by Google” on their Adsense units? It is no secret that Google has been tweaking its ad formats to squeeze every last cent out of them. Here’s a new one I hadn’t seen. I was looking at a page and saw “Ads by Google” and the Google was the stylized Catull font used for Google’s main header image. After refreshing the page, it was back to normal.

adsense-experiment.jpg

If this is old news to you, sorry. This is the first time I’ve seen it, and I’m pretty observant.

What will be next– The full-color Google logo in the ads?

ADDED: This is really strange. Now I’m seeing an ad block using the little favicon image instead of the word “Google”.

adsense-experiment-2.JPG

Advertising
Google

Comments (0)

Permalink

Registry key to disable the Windows key

This is more so I won’t forget it, and I’ll be able to find it when I need it. If you find it useful, great.

Disable both Windows keys

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
“Scancode Map”=hex:00,00,00,00,00,00,00,00,03,00,00,00,00,00,5B,E0,00,00,5C,E0,00,00,00,00

Disable the left Windows key

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
“Scancode Map”=hex:00,00,00,00,00,00,00,00,02,00,00,00,00,00,5B,E0,00,00,00,00

Disable right Windows key

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
“Scancode Map”=hex:00,00,00,00,00,00,00,00,02,00,00,00,00,00,5C,E0,00,00,00,00

Restore the Windows keys (remove scancode mappings)

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
“Scancode Map”=-

From John Haller via C# Shiznit.

Haller also has a post about how to disable the Caps Lock key.

Windows

Comments (0)

Permalink

301 Redirect in .htaccess file

In the last couple of days, I’ve gotten an inordinate number of requests about how to do a 301 permanent redirect from the non-www version of the person’s domain name to the www version. So instead of copying and pasting the same information over and over again, I can just point people here.

So if you’re using Apache and you want to do this redirect, put the following code in a file called .htaccess in the root directory of your website. Note that it’s more efficient to use httpd.conf file if you have access to it.


RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [L,R=301]

Webserver

Comments (0)

Permalink