PDA

View Full Version : Copy/Pasting Using Javascript?


Rekcor
10-06-2004, 10:27 AM
For my website, I want to make my own context menu, which pops up when the right-mouse-button is pressed. In this menu, I want to add some functions like Copy, Paste, Make Italic etc.

My question is: does somebody know javascript functions which simulate or induce the standard IE Copy/Paste functions which show up in the standard menu?

Kind regards,
Koos

maxadim
10-06-2004, 12:26 PM
Add this to your code:

Replace mytextbox and myform with your form tag and textbox id.

<head>
<script language="JavaScript" type="text/JavaScript">
<!--

function InsertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}

function PasteData(objName,x)
{
var content = clipboardData.getData("Text");
if (content!=null) { InsertAtCursor(document.myform.mytexbox, content);}
}

function CopySelectedData()
{
var CheckForSelection = window.external.menuArguments.document.selection.createRange().text;
window.clipboardData.setData("Text",CheckForSelection);
}

</script>
</head>

Then on the add this code to your buttons:
onClick="PasteData('mytexbox','')"
and
onClick="CopySelectedData()"

Hope this helps :)

Rekcor
10-11-2004, 06:59 AM
Thanx!