Link Clicking Before Javascript Loads (Problem)

自闭症网瘾萝莉.ら 提交于 2019-12-24 10:22:05

问题


I'm using shadowbox for some html/iframe links, which effectively opens up the page in a lightbox. Everything works great - except when someone clicks before the javascript is done loading.

Now, the obvious answer is to not use links as the targets of loading the shadowbox. But this poses usability problems if javascript is disabled. Does anyone have any ideas on how else to solve the problem? I'm considering loading javascript inline that would deactivate the links until the page is done loading, although I'm not exactly sure how I'd approach that.

All ideas are welcome!


回答1:


In general, I'd consider this to be a non-issue, but can't you just call the shadowbox processor inline directly after the links?

i.e.:

.link, .link, .link script $(".link").shadowbox(); /script




回答2:


You could define your links as

<a id="myLink" href="#" onclick="return false">...</a>

and then reassign the click event handler once your javascript is loaded. Or, if your links have non-JavaScript alternatives:

<a id="myLink" href="/path/to/nonJS/alternative.html">...</a>

Again, when your JavaScript loads you can add a click event handler and override the default action.

If it takes a while for all of your scripts to load, you might reconsider if you need to load all of them right away. If you do, then you could implement a loading screen (below example uses jQuery, but it doesn't have to):

<div id="loading">
    Loading...
</div>
<div id="content" style="display: none;">
  // Site content...
</div>
// Load your scripts here...
<script type="text/javascript" src="..."></script>
<script type="text/javascript">
  $(document).ready(function() {
    $('#loading').hide();
    $('#content').show();
  });
</script>

This way your site content isn't visible until all of your scripts are loaded.



来源:https://stackoverflow.com/questions/1916554/link-clicking-before-javascript-loads-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!