Javascript web page watermark (non-image watermark) implementation code

Source: Internet
Author: User

1 Overview
1.1 Definition
In some B/S architecture application systems, many pages require watermarks. Common examples are document systems and contract systems. We often focus on adding watermarks to website images, but on page watermarks. I just went to Google. Article The number is almost 0. In this article, the streamcompute Trojan will share with you about making Web watermarks.
This article discusses the following situation: the method for adding a watermark must be completed in JavaScript and must be easily added to the original page without affecting the existing functions.
1.2 expected goals
As for the image watermark implementation scheme, we expect at least the following goals:
1. Implement floating and translucent image watermarks
2. The page containing the watermark. All elements are read-only (not writable)
3. In the include frame page, you can control the watermark generation of any child or parent page.
4. after the page is zoomed in or out (resize process), you need to re-generate a new watermark to adapt to the page size without refreshing the page to ensure that all content is covered by the watermark, the scroll bar is not generated because the watermark image range is too large.
5. Supports IE6 \ 7 \ 8 browsers. Other browsers are not considered for the moment.
1.3
Before encryption:

After encryption:


You can open the file in the attachment for viewing.
2 implementation steps
2.1 Basic Ideas
The encryption process is a JavaScript function execution process, so we should first consider using JavaScript to operate DOM objects.
On the existing HTML page, create a DOM object under the body node. The length and width of the object are calculated to ensure that the scroll bar is not generated while overwriting all the page content. Overwrite the object to the original page, set the background image, and set it to transparent.
Create a new Dom element:
Use the createelement method in the Document Object. After an element is created, set its Z-index to a large integer to ensure that it can be larger than the maximum Z-index of an existing page to overwrite it ".
Calculate the size of the new object:
Use three DOM object values: clientwidth, scrollheight, and clientheight.
Generally, no horizontal scroll bar appears on the webpage, so scrollwidth is not used.
The vertical scroll bar is very common.
To ensure that all page content is overwritten, clientheight is used when no scroll bar appears. scrollheight is used when the scroll bar appears.
Transparent settings:
Use the Alpha value. Alpha is the CSS Filter supported by IE.
2.2 strain details
There is a small detail that is very interesting. As mentioned above, it is the resize process.
If a page is opened with 550px × 400px, A 550px × 400px watermark is generated. However, when you maximize the watermark, the page is not refreshed and the watermark generation function is not re-executed. Therefore, the watermark image produced previously is too small.
As shown in. Note that No watermarks are available on the right and bottom of the image.

To cope with this situation, we need to process the onresize () function of the body. If the previous definition does not have the onresize () function, add onresize () directly. If the previous onresize () function exists, modify it.
2.3 final code
This method includes three parameters: Target Page Object, target page string, and background image.

CopyCode The Code is as follows: function getwatermarked (targetobj, jpgurl, targetstr ){
VaR windowobj = targetobj;
VaR watermarkpicurl = jpgurl;
VaR controlwindowstr = targetstr;
If(windowobj.doc ument. getelementbyid ("watermark ")! = NULL)
Return;
VaR M = "watermark ";
VaR Newmark = windowobj.doc ument. createelement ("Div ");
Newmark. ID = m;
Newmark. style. Position = "absolute ";
Newmark. style. zindex = "9527 ";
Newmark. style. Top = "0px ";
Newmark. style. Left = "0px ";
Newmark. style. width = windowobj.doc ument. Body. clientwidth;
If (parseint(windowobj.doc ument. Body. scrollheight)> parseint(windowobj.doc ument. Body. clientheight ))
{
Newmark. style. Height = windowobj.doc ument. Body. scrollheight;
} Else
{
Newmark. style. Height = windowobj.doc ument. Body. clientheight;
}
Newmark. style. backgroundimage = "URL (" + watermarkpicurl + ")";
Newmark. style. Filter = "alpha (opacity = 50 )";
Windowobj.doc ument. Body. appendchild (Newmark );
VaR markstr = "Var sobj =" + controlwindowstr + ". document. getelementbyid ('watermark'); sobj. style. width = "+ controlwindowstr + ". document. body. clientwidth; sobj. style. height = "+ controlwindowstr + ". document. body. clientheight ;";
If(windowobj.doc ument. Body. onresize! = NULL)
{
VaR oldresiae = windowobj.doc ument. Body. onresize. tostring ();
VaR oldresiaestr = oldresiae. substr (oldresiae. indexof ("{") + 1 );
VaR oldresiaestr = oldresiaestr. substr (0, oldresiaestr. lastindexof ("}"));
Oldresiaestr + = ";" + markstr;
Windowobj.doc ument. Body. onresize = new function (oldresiaestr );
}
Else
{
Windowobj.doc ument. Body. onresize = new function (markstr );
}
}

3. original page processing
You must add an onload Method to the original <body> label. For example:
<Body onload = "getwatermarked(window,'watermark.jpg ', 'this')">
4 attachment
Demo address http://demo.jb51.net/js/js_wartermark/baidu_mark.htm
Attachment download
Http://xiazai.jb51.net/201003/yuanma/js_wartermark.rar

Related Article

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.