最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - prevent all MouseClick event until page load - Stack Overflow

programmeradmin7浏览0评论

I have a situation in which i have to prevent all MouseClick events until the page loads.

i have 1 javascript function defined on page load like

onload="init();"

Now in function init(), we are showing tree and select a particular node of it.

function init() {
        ExpandAncestors(node);
        ExpandNode(node);
        setTimeout("treeScrollToView()", 1000);
}

Now i want to prevent all the mouse click event on tree/page until whole tree is not fully shown.

I have searched through some of the posts related to my question but that uses event.preventDefault() but i dont have Event object here.

Any help is greatly appreciated.

I have a situation in which i have to prevent all MouseClick events until the page loads.

i have 1 javascript function defined on page load like

onload="init();"

Now in function init(), we are showing tree and select a particular node of it.

function init() {
        ExpandAncestors(node);
        ExpandNode(node);
        setTimeout("treeScrollToView()", 1000);
}

Now i want to prevent all the mouse click event on tree/page until whole tree is not fully shown.

I have searched through some of the posts related to my question but that uses event.preventDefault() but i dont have Event object here.

Any help is greatly appreciated.

Share Improve this question asked Apr 27, 2015 at 4:19 Sanket PipariyaSanket Pipariya 891 gold badge1 silver badge12 bronze badges 1
  • THIS LINK might be helpful too.. – Guruprasad J Rao Commented Apr 27, 2015 at 4:25
Add a ment  | 

5 Answers 5

Reset to default 10

You can use:

CSS

body {
  pointer-events:none;
}

and then on page load reactivate them

$(document).ready(() => {
  $('body').css('pointer-events', 'all') //activate all pointer-events on body
})

Explanation

pointer-events:none; blocks all mouse interaction with the elements it's applied to - Since the body is usually the parent of all the elements in your page, it would case them not to react to any mouse interaction at all.

Keep in mind that all mouse interaction would be blocked this way, not only mouse clicks but mouse hover, mouse up's etc etc..

I think the basic need is to prevent user from clicking the tree area. I would prefer to display an overlay div rather than playing with the tree mouse click events.

You can show a loading overlay on the tree part until it is loaded. once done, you can hide the loading and show your original tree.

Ref: How to pletely DISABLE any MOUSE CLICK

JavaScript Only

You can have an event listener along with a boolean. onclick disables a click. oncontextmenu disables right clicks.

(function(){window.onload = function () {

    var allowClicks = false;

    document.onclick = function (e) {  !allowClicks&&e.preventDefault();  }
    document.oncontextmenu = function (e) {  !allowClicks&&e.preventDefault();  }
    document.getElementById('myElement').onload = function () { allowClicks = true; }

}());

myElement is your element which you can replace with whatever


Use this with one element

If you want to disable mouse clicks for just one element, do:

(function(){window.onload = function () {

    var allowClicks = false,
        elem = document.getElementById('myElement');

    elem.onclick = function (e) {  !allowClicks&&e.preventDefault();  }
    elem.oncontextmenu = function (e) {  !allowClicks&&e.preventDefault();  }
    elem.onload = function () { allowClicks = true; }

}());

onload="init();" here you can have event object.

pass event as argument.

onload="init(event);"

now you can use that in init() function.

Try utilizing $.holdReady()

$.holdReady(true);

$(window).off("click");

$("*").each(function(i, el) {
  this.onclick = function(e) {
    e.preventDefault();
  }
});

 function init() {
  $("div").on("click", function() {
    alert($.now())
  })
}

setTimeout(function() {
  $.holdReady(false);
}, 7000)

$(function() {
    init()
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<body>
<div>click</div>
  <a href="javascript:alert(new Date())">click</a>
</body>

发布评论

评论列表(0)

  1. 暂无评论