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

javascript - Prevent clicking on link while page loading - Stack Overflow

programmeradmin3浏览0评论

I have a web page with some links, and I need to intercept the link-clicking event by jQuery. The task is finished well, but then a problem arises: if the user click a link when JavaScript doesn't finish loading, it link to another page (which is an error).

I have tried to find a way to disable link-clicking before page loading finish, but the best solution now is that I must add onclick="return false;" into my links, which is not very elegant. Is there any better solution?

I have a web page with some links, and I need to intercept the link-clicking event by jQuery. The task is finished well, but then a problem arises: if the user click a link when JavaScript doesn't finish loading, it link to another page (which is an error).

I have tried to find a way to disable link-clicking before page loading finish, but the best solution now is that I must add onclick="return false;" into my links, which is not very elegant. Is there any better solution?

Share Improve this question edited Sep 3, 2022 at 16:11 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 8, 2011 at 3:24 Hoàng LongHoàng Long 10.8k21 gold badges79 silver badges127 bronze badges 2
  • 3 Why not send the href as another attribute, and then copy that attribute to href when the link is ready to be clicked on? – jrockway Commented Feb 8, 2011 at 3:32
  • @jrockway: thanks for the remendation, but I think it's not convenient to do that. – Hoàng Long Commented Feb 8, 2011 at 4:53
Add a ment  | 

4 Answers 4

Reset to default 6

You could try using an initialization script:

<script language="javascript">

var loaded = false;

function SetLoaded() { loaded = true; }

window.onload = SetLoaded;

</script>

Then you can add onclick="return loaded;" to your href. Since loaded won't be true until the page loads, it should disable any link with this added.

Today i was facing this issue so i did R&D and figured out another way of it. document.readyState will be 'plete' after page load so you can check its value like.

 if(document.readyState=='plete')
 {
   //// call script or do anything which you want to do.
 }

Have jQuery set up the hrefs in the links in your document.ready(). Maybe prefix your links with a '#', and have jQuery strip them out.

Here is a solution I have worked out: Using a masker put in front of the link to prevent clicking event.

<div class="LinkMasker"></div>

.LinkMasker {
    position: absolute;
    z-index: 999;
    width: ...px;
    height: ...px;
}

After page loading & my event are all registered, I remove the masker:

$('.LinkMasker').remove();
发布评论

评论列表(0)

  1. 暂无评论