user6107006 user6107006 - 4 months ago 21
PHP Question

(php - javascript) popup when opening website on mobile

How can I make popup ads work only one time for mobile ios browser.

I am using (popup show every time : how I can make it show only first time)

<script type="text/javascript">
if( /iPad|iPhone/i.test(navigator.userAgent) ) {
var url=confirm("Would you like to download our mobile application?");
if (url==true)
{
var url = window.location.href = 'http://itunes.apple.com/us/app/AppId';
url.show();
}
else
{
}
}
</script>

Answer

You should use cookies to store a value that the popup has been shown then add check to your popup that if the cookie exists then don't display the popup. I updated your code to reflect the changes you need to make, goodluck :)

  <script type="text/javascript">
    if( /iPad|iPhone/i.test(navigator.userAgent) ) {

        //cookie name mypopup not found so display the popup
       if(!getCookie("mypopup")){
            setCookie("mypopup",true); //sets the cookie mypopup
            var url=confirm("Would you like to download our mobile application?");
            if (url==true)
            {
                var url = window.location.href = 'http://itunes.apple.com/us/app/AppId';
                url.show(); 
            }else{


            }
       }

    }

//function to retrieve cookie value
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length,c.length);
        }
    }
    return "";
}

//function to set the cookie
function setCookie(cname, cvalue) {
    document.cookie = cname + "=" + cvalue + "; ";
}
    </script>
Comments