상세 컨텐츠

본문 제목

[JavaScript] 자바스크립트 마우스오른쪽 컨텍스트 메뉴 안나오게하기 관련

WEB/JavaScript

by AlrepondTech 2017. 12. 6. 13:59

본문

반응형
728x170

 

 

 

=================================

=================================

=================================

 

 

 

 

 

 

출처 : https://stackoverflow.com/questions/4909167/how-to-add-a-custom-right-click-menu-to-a-webpage

 

 

 

196down voteaccepted
Answering your question - use contextmenu event, like below:
<html> <head> <script type="text/javascript"> if (document.addEventListener) { // IE >= 9; other browsers document.addEventListener('contextmenu', function(e) { alert("You've tried to open context menu"); //here you draw your own menu e.preventDefault(); }, false); } else { // IE < 9 document.attachEvent('oncontextmenu', function() { alert("You've tried to open context menu"); window.event.returnValue = false; }); } </script> </head> <body> Lorem ipsum... </body> </html>
But you should ask yourself, do you really want to overwrite default right-click behavior - it depends on application that you're developing.
 

 

ES6 버전으로 바꾸어 보겠다.

 

 

 

//ES6 Class Version, add On, Off


// private properties:
let priCSMsgLis = {
_bShowSysContextMenu: Symbol(),
};

export class CContextMenu
{
constructor()
{

//기본상태는 팝메뉴를 보여주는 것으로 한다.
this[priCSMsgLis._bShowSysContextMenu] = true;

this.init();
}

init()
{

//자신이 쓰는 domElement 객체를 넣어주면 된다.

//여기서는 스크립트 전역에 쓰는 document

을 쓰겠다.

let dom = document;

this.initContextMenuLis(dom);
}

initContextMenuLis(dom)
{
let me = this;

if (dom.addEventListener)
{ // IE >= 9; other browsers
dom.addEventListener('contextmenu', function(e)
{
if(!me.isShowSysContextMenu)
{
//alert("You've tried to open context menu"); //here you draw your own menu
e.preventDefault();
}
}, false);
}
else
{ // IE < 9
dom.attachEvent('oncontextmenu', function()
{
if(!me.isShowSysContextMenu)
{
// alert("You've tried to open context menu");
window.event.returnValue = false;
}
else
{
window.event.returnValue = true;
}
});
}
}

showSysContextMenu(bOn)
{
this[priCSMsgLis._bShowSysContextMenu] = bOn;
}

get isShowSysContextMenu()
{
return this[priCSMsgLis._bShowSysContextMenu];
}

}

 

 

 

 

 

 

 

=================================

=================================

=================================

 

 

 

출처: http://superkts.pe.kr/helper/view.php?seq=V&seq=206

 

 

마우스 오른쪽 클릭후 뜨는 메뉴를 안뜨게 하는방법입니다.
body 태그에 oncontextmenu="return false" 하면 됩니다만

이 예제는 body 태그에 스크립트로 oncontextmenu 를 설정해 줍니다.

 

 

=================================

=================================

=================================

 

 

 

반응형
그리드형


관련글 더보기

댓글 영역