Thursday, February 28, 2008

Block images on the page from being copied

Recently my client wanted to block images on the page from being copied. So we wrote a standard script to trap and block right-click on a page. But he did not wish to block entire page, he wanted users to copy unique Itemcode and a couple of other unique items on the page for correspondence. Well we can decided to block right-click only for images This is how we ejected a client script to achieve the task.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim s As New StringBuilder()
s.AppendLine("function ProtectImages(e) {var msg = 'Warning: Image is copyrighted.';")
s.AppendLine("if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2){")
s.AppendLine("return false;}")
s.AppendLine("else return true;}")

s.AppendLine("if (document.images){")
s.AppendLine("for(i=0;is.AppendLine("document.images[i].onmousedown = ProtectImages;")
s.AppendLine("document.images[i].onmouseup = ProtectImages; }}")

ClientScript.RegisterStartupScript(Me.GetType(), "ProtectImages", s.ToString(), True)
End Sub

No comments: