Just another WordPress.com site

We use pre tags to display the embed codes and short URLs. We use pre tags because it formats the text inside the tags as we insert it, which is ideal for displaying code in particular. The thing is, people like to copy this code and they have to go to the awful bother of selecting all the text manually if they wish to copy the code or URL.

jQuery hack to auto select all the text inside the pre tags. The only complication with this appears to be a cross platform solution as each browser appears to have their own way of selecting text.

Internet Explorer uses createTextRange.
Opera and Firefox use createRange.
Safari uses DOMSelection.
So with a pre tag like (just click anywhere in the code)…

jQuery( document ).ready(function() { jQuery( ‘pre.code’ ).click( function() { var refNode = $( this )[0]; if ( $.browser.msie ) { var range = document.body.createTextRange(); range.moveToElementText( refNode ); range.select(); } else if ( $.browser.mozilla || $.browser.opera ) { var selection = window.getSelection(); var range = document.createRange(); range.selectNodeContents( refNode ); selection.removeAllRanges(); selection.addRange( range ); } else if ( $.browser.safari ) { var selection = window.getSelection(); selection.setBaseAndExtent( refNode, 0, refNode, 1 ); } } ); } );

jQuery( document ).ready(function() {	
jQuery( 'pre.code' ).click( function() {
var refNode = $( this )[0];
if ( $.browser.msie ) {
var range = document.body.createTextRange();
range.moveToElementText( refNode );
range.select();
} else if ( $.browser.mozilla || $.browser.opera ) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents( refNode );
selection.removeAllRanges();
selection.addRange( range );
} else if ( $.browser.safari ) {
var selection = window.getSelection();
selection.setBaseAndExtent( refNode, 0, refNode, 1 );
}
} );
} );

here goes the code :

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js&#8221; type=”text/javascript”></script>
<script>
jQuery( document ).ready(function() { jQuery( ‘pre.code’ ).click( function() {
var refNode = $( this )[0];
if ( $.browser.msie ) {
var range = document.body.createTextRange();
range.moveToElementText( refNode );
range.select();
} else if ( $.browser.mozilla || $.browser.opera ) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents( refNode );
selection.removeAllRanges();
selection.addRange( range );
} else if ( $.browser.safari ) {
var selection = window.getSelection();
selection.setBaseAndExtent( refNode, 0, refNode, 1 );
}
} );
  } );
</script>
</head>
<body>
<pre class=’code’>jQuery( document ).ready(function() { jQuery( ‘pre.code’ ).click( function() {
var refNode = $( this )[0];
if ( $.browser.msie ) {
var range = document.body.createTextRange();
range.moveToElementText( refNode );
range.select();
} else if ( $.browser.mozilla || $.browser.opera ) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents( refNode );
selection.removeAllRanges();
selection.addRange( range );
} else if ( $.browser.safari ) {
var selection = window.getSelection();
selection.setBaseAndExtent( refNode, 0, refNode, 1 );
}
} );
  } );
</pre>
</body>
</html>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: