Welcome to WebmasterWorld Guest from 54.166.48.3

Forum Moderators: open

Message Too Old, No Replies

Remove Formatting Using Javascript

     
8:30 pm on Jun 1, 2012 (gmt 0)

New User

5+ Year Member

joined:Sept 18, 2010
posts: 15
votes: 0


I have been working on modifying a javascript that will allow users of my website to copy/paste without bringing along all those HTML formats. I am not fluent with Javascript so for me it is a lot of hit and miss, usually with mostly misses. The script was originally designed to add a copywrite notice and link to any copy and pasted text. One problem I have been having I was able to do a sort of patch for instead of an actual fix. I changed the background color to white because the webpage body is gray.

What I want to do is instead of CHANGE colors, or fonts, or whatever, I want to just DROP all those unwanted formats sort of like right clicking and paste as unformatted text. I would sure welcome any suggestions or help anyone could offer.

So far this is what I have:

<script type="text/javascript">
function Format() {
var body_element = document.getElementsByTagName('body')[0];
var selection;
selection = window.getSelection();
var copytext = selection;
var newdiv = document.createElement('div');
newdiv.style.background = "white";
body_element.appendChild(newdiv);
newdiv.innerHTML = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout(function() {
body_element.removeChild(newdiv);
},0);
}
document.oncopy = Format;
</script>
7:26 am on June 9, 2012 (gmt 0)

New User

joined:June 9, 2012
posts:4
votes: 0


I'm not too sure what you mean by "HTML formats". Nevertheless, besides IE, there is no way to access text that has been copied to clipboard in JavaScript. If I recall correctly, you can capture selected text, but that will not affect anything when the user copies and pastes.