简体   繁体   English

当离线或在线连接到互联网时,如何更改我的图片,

[英]How can i change my image when connection to internet is fired offline or online,

        <script>


            function myFunction() {

                if (navigator.onLine) {
                    swal("Great News" , 'Congratulation your connection is online', "success");
                } else {
                    swal("Sad News" , 'Can you please connect to the internet to login', "error");
                }

            }

            </script>

I would like to add a function whereby the image is changed depending whether the users connection to the internet is offline or online 我想添加一个功能,通过该功能可以更改图像,具体取决于用户与Internet的连接是脱机还是在线

you can set a custom function like 您可以设置自定义功能,例如

 window.addEventListener("online" , _=>{
            //set image online
        })
        window.addEventListener("offline" , _=>{
            //set image offline
        })

like 喜欢

 var img1 = document.getElementById("wifi-image") function changeimage(online){ if(online) { img1.src ="online-wifi.png" img1.alt ="online-wifi.png" } else{ img1.src ="offline-wifi.png" img1.alt ="offline-wifi.png" } } window.addEventListener("online" , _=>{ //set image online changeimage(true) }) window.addEventListener("offline" , _=>{ //set image offline changeimage(false) }) // at start changeimage(navigator.onLine) 
 <img id="wifi-image" src="" alt="online-mode"> 

As I can see that you have mentioned Cordova tag, So I'm assuming that it is for a mobile app. 正如我所看到的,您提到了Cordova标签,所以我假设它是针对移动应用程序的。

You can store the image in the cache by using imgcache 您可以使用imgcache将图像存储在缓存中

https://github.com/chrisben/imgcache.js/ https://github.com/chrisben/imgcache.js/

but this will not work if your app has not connected to the internet even once because then it'll not have a file for reference. 但如果您的应用程序甚至没有连接到互联网,这将无法正常工作,因为那样便没有文件可供参考。

Let me know if you find any difficulties. 如果您有任何困难,请告诉我。

cheers 干杯

in HTML : 在HTML中:

<img id="imgID" src="img.png"/>

in JavaScript : 在JavaScript中:

if(navigator.network.connection.type == Connection.NONE){
   console.log("device is offline");
    //you can now call picture from your folder
    $("#imgID").attr("src","img.png");

}else{
   console.log("device is online");
   //you can now call picture from internet like this
   $("#imgID").attr("src","https://website.com/img.png");
}

暂无
暂无

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

相关问题 Ionic3 /当连接从离线更改为在线时,如何让应用尝试加载图像? - Ionic3 / How to let app try to load the image when connection changed from offline to online? 根据外部 Internet 连接动态更改状态 - React(离线/在线) - Change state dynamically based on the external Internet connectivity - React (offline/online) 如何从离线访问在线JSON文件? (JavaScript的) - How can I access an online JSON file from offline? (javascript) 在MVC中更改页面时,如何动态更改POPOVER中的图像路径? - how can I dynamically change the image path in my POPOVER when I change my page in MVC? 当在Dynamic 365 Online中触发OnStageChange事件时,如何确定用户是转到业务流程的下一阶段还是上一步? - How can I tell if user is moving to next or previous stage of business process flow when OnStageChange event is fired in Dynamic 365 Online? 检测到互联网连接是否离线? - Detect the Internet connection is offline? 如何触发我的在线和离线事件 - how to fire my online and offline events 当同时触发多个更改事件时,如何防止函数多次运行? - How can I prevent a function from running more than once when multiple change events are fired at once? angular怎么查询离线和在线 - How can check offline and online in angular 如何确保我的应用程序可以在没有互联网连接的情况下与外部库(例如Google Maps)一起使用? - How can I ensure that my app works with external libraries like Google Maps without an internet connection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM