Automatically removes the junk code from the contents of the copy Word

Source: Internet
Author: User
Tags copy return
Word

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta name= "generator" content= "EditPlus" >
<meta name= "Author" content= "" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" >
</HEAD>

<BODY>
<script>
function getclipboardhtml ()
{
var odiv = document.getElementById ("Divtemp")
odiv.innerhtml = "";

var otextrange = Document.body.createTextRange ();
Otextrange.movetoelementtext (ODIV);
Otextrange.execcommand ("Paste");

var sData = odiv.innerhtml;
odiv.innerhtml = "";

return sData;
}


function Cleanandpaste (HTML)
{
Remove All SPAN Tags
html = html.replace (/<\/? Span[^>]*>/gi, "");
Remove Class Attributes
html = html.replace (/< (\w[^>]*) class= ([^ |>]*) ([^>]*)/gi, "<$1$3");
Remove Style Attributes
html = html.replace (/< (\w[^>]*) style= "([^"]*) "([^>]*)/gi," <$1$3 ");
Remove Lang Attributes
html = html.replace (/< (\w[^>]*) lang= ([^ |>]*) ([^>]*)/gi, "<$1$3");
Remove XML Elements and declarations
html = html.replace (/<]*>/gi >)\\?\?xml[^>]*>/gi, "") ;
Remove Tags with XML namespace declarations: <o:p></o:p>
html = html.replace (/<\/?\w+:[^>]*>/gi, "");
Replace the
HTML = html.replace (//, "");
Transform <P> to <DIV>
var re = new RegExp (<p) ([^>]*>.*?) (<\/P>) "," GI ")//different because of a IE 5.0 error
html = html.replace (Re, "<div$2</div>");

inserthtml (HTML);

return HTML;

Form1.my.value = html;
}

function Paste ()
{
var SHTML = getclipboardhtml ();

var re =/<\w[^>]* class= "? MsoNormal "?/gi;
var Re2 =/<\w[^>]* class= "? xl"?/gi;
if (Re.test (SHTML))
{
Alert ("The content you want to paste appears to be from MS Word, the system clears MS Word formatting and then pastes it!") ")
}

if (Re2.test (SHTML))
{
Alert ("The content you want to paste appears to be from MS Excel, the system clears MS Excel formatting and then pastes it!") ")
}
Cleanandpaste (SHTML);
}
</script>
<form id= "Form1" name= "Form1" >
<input type= "button" value= "Paste" >
<br/>
<br/>
<textarea name= "My" cols= "rows=" ></textarea>
<div id=divtemp
Style= "Visibility:hidden; Overflow:hidden; width:1px; Position:absolute; height:1px "></DIV>
</Form>
</BODY>
</HTML>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.