简体   繁体   English

Javascript Div加载图像无法正常工作

[英]Javascript Div Loading Image not working properly

I have written a code in javascript : 我已经用javascript编写了代码:

  1. When user click on a link, a loading image should be shown till next page loads. 当用户单击链接时,应该显示加载图像,直到加载下一页。

  2. It is working fine when user simply click on link, Loading image is visible till next page loads. 当用户简单地单击链接时,它工作正常,直到下一页加载时,加载图像才可见。 But the problem is when a user clicks on link with CTRL button. 但是问题是当用户单击带有CTRL按钮的链接时。 target page open in new tab of browser and on page from where link was click, loading div continuously shown. 在浏览器的新标签页和单击链接的页面上打开目标页面,并持续显示加载div。

 function ShowLoading(e) { var div = document.createElement('div'); var img = document.createElement('img'); img.src = 'pageloaderB.gif'; img.style.cssText = 'border:0px'; div.innerHTML = "<b style=color:black;font-size:40px;font-family:calibri>Processing Request Please Wait</b><br><br><br>"; div.style.cssText = 'padding-top:200px;position: fixed; top: 0px; left: 0px; z-index: 5000; width: 100%; height:100%; text-align: center; background:white;opacity: .8;'; div.appendChild(img); document.body.appendChild(div); return true; } 
 <a href="dashboard.php" class="ico1" onclick="ShowLoading()">Dashboard</a> 

Javascript: Javascript:

Please help me to close this. 请帮助我完成此操作。 Either simple click or CTRL+Click, Loading div must be invisible when page loads completely. 简单单击或CTRL + Click,当页面完全加载时,“加载div”必须不可见。

Use localStorage to introduce a "global" variable, ie one that spans multiple tabs. 使用localStorage引入“全局”变量,即跨多个选项卡的变量。

When a link is clicked, you set localStorage['loading'] to true, and in each page's window.onload event you set it back to false. 单击链接后,将localStorage['loading']为true,然后在每个页面的window.onload事件中将其设置为false。

When you show the image, you start an Interval that keeps checking the variable. 显示图像时,您将启动一个Interval,它会不断检查变量。 When the page in the new tab has loaded and the variable is set to false, the interval function will hide the image and stop its interval. 当新标签页中的页面已加载且变量设置为false时,interval函数将隐藏图像并停止其间隔。

(This answer is much more convoluted than simply checking whether the Ctrl key was pressed, but it will even work with a middle mouse click or other ways of opening a link in a new tab.) (与简单地检查是否已按下Ctrl键相比,这个答案要复杂得多,但是甚至可以通过单击鼠标中键或其他方式在新标签页中打开链接来解决。)

You can examine the Ctrl, Shift, and Meta key properties of the event object. 您可以检查事件对象的Ctrl,Shift和Meta键属性。 If either is true, the key control, shift, or meta (Apple's command) key is being held. 如果其中一个为真,则将按住键控制,Shift或meta(Apple的命令)键。 You'll find a useful JavaScript snippet that will help with solving the problem here: How to detect if user it trying to open a link in a new tab? 您将在此处找到有用的JavaScript代码段,该代码段有助于解决问题: 如何检测用户是否试图在新标签页中打开链接?

Instead of using onclick of link use event beforeunload . 而不是使用链接的onclick使用事件beforeunload So this will avoid unnecessary call to ShowLoading . 因此,这将避免不必要地调用ShowLoading

In case of plain javascript: 如果是普通的javascript:

window.onbeforeunload=ShowLoading;

With jQuery: 使用jQuery:

$(window).on('beforeunload',ShowLoading);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM