www.webdeveloper.com
Results 1 to 3 of 3

Thread: nomousedown confined to a specific tag?

Hybrid View

  1. #1
    Join Date
    Jun 2013
    Posts
    3

    nomousedown confined to a specific tag?

    Having RTFMed and searched, I am still not sure that it is possible. It seems it should be, but what I tried doesn't work (probably due to the fact that I am new to Javascript).

    I am formatting training lab books using XHTML with XML custom tags. I want to give students a reminder that copying and pasting commands is not conducive to learning, so I want to disable right mouse click, but only when they try to copy a command contained in a specific tag. Here is the script I am trying:

    HTML Code:
    //<![CDATA[
    document.createElement ("pnum")
    pnum.onmousedown=disableclick;
    status="Copying and pasting commands is detrimental to learning";
    function disableclick(e)
    {
      if(event.button==2)
       {
         alert(status);
         return false;    
       }
    }
    //]]>
    It works perfect without the first line and with "document.onmousedown" in the second, but that affects entire document. Is onmousedown only possible within document and window, or am I missing something obvious?

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Code:
    //<![CDATA[
    document.createElement ("pnum")
    pnum.onmousedown=disableclick;
    status="Copying and pasting commands is detrimental to learning";
    function disableclick(e)
    {
      if(event.button==2)
       {
         alert(status);
         return false;    
       }
    }
    //]]>
    You would need to attach pnum to the document and even then it would only work on that element. Presumably you want it to work over any <pnum> tag. Also you should use the oncontextmenu event.
    Code:
    (function()
    {
      function f( evt )
      {
        var srcElem = evt.target || evt.srcElement;
            
        while( srcElem && srcElem.nodeName != 'PNUM' )
          srcElem = srcElem.parentNode;
          
        if( srcElem )
        {
          evt.preventDefault ? evt.preventDefault() : evt.returnValue = false; 
          alert('Copying and pasting commands is detrimental to learning');       
        }
      }
      
      document.addEventListener ? document.addEventListener( 'contextmenu', f, false ) : document.attachEvent( 'oncontextmenu', f );   
    })();

  3. #3
    Join Date
    Jun 2013
    Posts
    3
    Thanks, Logic Ali. It works perfectly. I think it will take me a while to digest and understand.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles