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

What is CSS?

This is a mercy post for those of you who don’t have a clear understanding of what CSS is.

I’m not really familiar with css other than hearing that it’s better than html because of the <tables> vs <div> tags.

The previous quote was found on a freelance coding site and really highlights the fact that there is a lot of misunderstanding out there about CSS.

CSS does not replace HTML, it augments it.

HTML:

<div>This is a sentence</div>

HTML with CSS:

<div style=”border:1px #F00 solid;”>This is a sentence</div>

Just as I used CSS with this <div> tag, I can use CSS with a table. The dichotemy that this user mentions between <div>s and <table>s is that for many years tables have been (wrongly*) used for formatting, and by combining CSS with <div>s you can do similar formatting without using tables.

* I agree that tables are not the best solution for formatting issues, I don’t feel you should use 30k of complicated HTML/CSS/CSS hacks to replace a 12k tables-formatted design. :)

CSS
HTML

Comments (0)

Permalink

HTML entity for curly quotes

Some of these posts are just going to be reminders for me. That way I can just pump in a quick search in the box above and find what I need.

So… tada… here are the HTML entities for curly quotes:

Desc. HTML or XML entity Displays as
Double left curly quotation (XML) &#8220;
Double left curly quotation (HTML) &ldquo;
Double right curly quotation (XML) &#8221;
Double right curly quotation (HTML) &rdquo;
Single left curly quotation (XML) &#8216;
Single left curly quotation (HTML) &lsquo;
Single right curly quotation (XML) &#8217;
Single right curly quotation (HTML) &rsquo;

HTML
XML

Comments (0)

Permalink

Hello world!

I thought I should go back and edit this post. It’s Feb 4th 2007 and I truly cannot believe I installed Wordpress with the intention of blogging regularly back in 2005 and here it is 2007 and I’m barely starting. Oh well. Better late than never right?

About

Comments (1)

Permalink