1

I use this code to prevent the visitors of my website to copy-paste its content.

<script>document.addEventListener('contextmenu', event => event.preventDefault());</script>
<script>element.on(?:copy|cut|paste)</script>

I was wondering if it were possible to exclude one URL from this script? It would help since it's for the tracking and customers would use copy-paste

Let me know, thanks you in advance :)

6
  • 1
    A better idea might be to just not include the script on the page you want to exclude it from. Commented Mar 19, 2018 at 17:04
  • You can get the current url by checking window.location. Commented Mar 19, 2018 at 17:04
  • The second one won't work anyway. And why not just not include the script when you don't want it? What prevents you from doing that? Commented Mar 19, 2018 at 17:04
  • Also understand that people can easily get around a mechanism like that. Commented Mar 19, 2018 at 17:05
  • Generally there is no any javascript will prevent user from copying contents, simply, they may disable it or select and CTRL+C. However, you may call this functionality from a function in which a check for document.location occurred before listen to the event. Commented Mar 19, 2018 at 17:06

1 Answer 1

1

Instead of

document.addEventListener('contextmenu', event => event.preventDefault());

you could do something like

document.addEventListener('contextmenu', event => {
    if (window.location.href !== 'http://example.com/exclude/') {
        event.preventDefault()
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, I even found a better way: exclude the paste event !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.