jquery - hide after reload

I have this jquery code and every things working fine but after window.location.reload(true); I want to hide the div #cmd if possible

$(document).ready(function () {
    $("#add").click(
        function () {

            var id = $(this).attr("rel"); // ID
            var full_name = $("#full_name").val();
            var email = $("#email").val();
            var comments = $("#comments").val();
            var datapost = "id=" + id + "&full_name=" + full_name + "&email=" + email + "&comments=" + comments;

            $.ajax(
                {

                    type:"post",
                    url:"{/literal}{base_url()}{literal}post/add_cmd",
                    data:datapost,
                    success:function (Response) {
                        if (Response == 'added') {

                            alert("شكرا لك .. " + full_name + " على التعليق سوف يتم إداراج تعليقك بعد تحديث الصفحة");
                            window.location.reload(true);
                            $("#cmd").hide("Slow");

                        } else {
                            alert(Response);
                        }
// window.location.href = '../admcat/cats';
                    },
                    fail:function () {
                        alert("تعذر حذف الركورد لسوء الاتصال بالانترنت");
                    }
                }
            );


        }
    );


});

I want after window.location.reload(true); will hide the $("#cmd") if possible.

Upvotes: 1

Views: 1141

Answers (2)

nalply
nalply

Reputation: 28767

This is not so easy, because HTTP is a stateless protocol. That means, if you reload, the state of the page got lost.

But there's a way when you use JavaScript and PHP together.

  1. Set a cookie on the first load
  2. On the reload in PHP check existence of the cookie and set visibility of the #cmd div.

There are other ways. All these ways try to pass the state ("page already loaded once") to the next load.

  1. Reload using a different method (first time GET, then POST)
  2. Pass an URL parameter instead of just reloading
  3. Use PHP session

All these four ways need PHP to check: Does the cookie exist, what's the HTTP method, what's the parameter, what's in the session?

Upvotes: 2

Christofer Eliasson
Christofer Eliasson

Reputation: 33865

There is no good way to detect if it was a page load or reload, it is basically the same thing. If you want to hide the element both when the page is first loaded, and then on every reload, you could do something like this:

$("#cmd").hide();

If it have to be on reload only, I guess one way would be to not use window.location.reload but window.location.href instead, and add something to the URL, like a querystring variable, that you then can check for on each page load, and hide the element if present.

Upvotes: 2

Related Questions