user10303291
user10303291

Reputation:

Create a javascript Cookie for my template

here is my javascript code

<script type="text/javascript">

function JSalert(){
    swal({   title: "Are Your age 18+!",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#009688",
    confirmButtonText: "Yes, Iam 18+ !",
    cancelButtonText: "No, I am not sure!",
    closeOnConfirm: false,
    closeOnCancel: false },
    function(isConfirm){
        if (isConfirm)
    {
        swal("Wellcome !", "Enjoy with us!", "success");
        } 
        else {
            swal("Hurray", "Please Go Back!", "error");
            } });
}
</script>

when I reload this page ...Every time its seen: enter image description here

Can anyone help me to see it One Time !!

Upvotes: 0

Views: 233

Answers (1)

Shanu T Thankachan
Shanu T Thankachan

Reputation: 1073

Set a cookie when you show the alert. Then, if the cookie is set, you will know not to show the alert again. If it is not set, you know that you haven't shown the alert yet and should do so now.

You can read about setting cookies in JavaScript here.

  function doOnce() {
  if (document.cookie.replace(/(?:(?:^|.*;\s*)doSomethingOnlyOnce\s*\=\s*([^;]*).*$)|^.*$/, "$1") !== "true") {
    alert("Do something here!");
    document.cookie = "doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT";
  }
}

<button onclick="doOnce()">Only do something once</button>

Upvotes: 1

Related Questions