Search code examples
javascripthtmlmobile-website

Is there a way to ask the user before loading the website


I have recently been putting together a website which is incredibly content heavy, and I am wanting to prepare it for mobile viewing. However, one thing I am concerned about is that if the user decides to view the page while not connected to a WiFi network the website will destroy their mobile data plan.

What I was thinking then was to create a pop up message before any content is loaded asking the user if they would like to continue loading the website, and if they choose not to, to close the website. The pop up does not need to happen when the user is on a desktop, as they are more likely to be connected to a WiFi network.

Is this a good idea, and is it possible? Are there better methods? Or is this something that I should not be concerned about?

I only really know how to use Javascript, so if the solution is Javascript related, then that'd be perfect. However if it requires something else, please let me know.


Solution

  • Just put this in the first line of your javascript code

    let close = confirm("This website is data heavy, if you have a limited data plan click OK") == true ? window.top.close() : "Do nothing";
    

    It simply asks the user to confirm that they want to continue and the ternary statement checks if they said no and if they did it closes that tab.