Short tutorials on Web Development

Status
Not open for further replies.

rohan

In the zone
I had many tips and tricks in my mind... but giving them each a seperate new topic would be like spamming.. so I'll make a collection of simple and short tutorials here. Hope it helps you. I'll add more as I write more.

WDST 1:
Name: Using the clipboard from JavaScript
Platform-dependency: Works only in Internet Explorer 5+

This is a small tutorial form my side on how to use the clipboard from JavaScript. For those coming in late, the clipboard is a software concept where data is temporarily stored for users to access from within application i.e when you copy/paste things... data is written to/read from the clipboard. Here is a short tutorial on how to use the clipboard.

READ: You must be having basic knowledge of JavaScript before proceeding with this tutorial. The code mentioned here must be placed in the <script></script> HTML containers to work.

1. Getting data from clipboard ("Paste")

var clip = window.clipboardData;

Here, 'window.clipboardData' is an object which gives you access to the clipboard. It has two functions:

getData(datatype); //returns the data of the clipboard
setData(datatype, data); //sets the data on the clipboard

Now, we can use:

var clip = window.clipboardData;
var clipdata = clip.getData("Text");
alert(clipdata);

Here, we get the data from the clipboard to a variable called 'clipdata', which we then output by using the 'alert' function. You can also use the variable further anyway you want it.

now, why did we use "Text"... that specifies the datatype of the data we need. We can use "html", "Image" and a few other datatypes.. but "Text" is what you'll commonly use.

2. Putting data into clipboard ("Copy")

it's as simple as that:

var clip = window.clipboardData;
clip.setData("Text", "This is what you'll see in the clipboard");

Now, open any application which has copy/paste features and try pasting it... it'll paste "This is what you'll see in the clipboard".

NOTE: The window.clipboardData object is available only in Internet Explorer, it will not work in any other browsers. Since the browser scope is limited, it is generally used for making HTML applications like .hta one's which are meant to run with only Internet Explorer. Anyways in the future we can expect more browsers to suppor this feature.

======================================

Expect more soon ;)
 
Status
Not open for further replies.
Top Bottom