ruby - Rails 4/Cookies - Load modal (by clicking on button) only on user's first page view -
in ruby on rails app, load modal when user loads page first time.
i decided use cookies , set cookie(user saw once page) = true setting value after page has been loaded.
this way, user loads page first time, views loads (cookie value still false), cookie set 'true' , second time load page, won't load modal.
but not work. think cookie gets set before view loaded altough have placed code lines after respond_to , @ end of contorller action showcase (see below).
controller 'deal'
def showcase # load page @deal = deal.friendly.find(params[:id]) respond_to |format| format.html # showcase.html.erb format.json { render json: @deal } end cookies.permanent[:modal_loaded_once] = true end view showcase.html.erb
//this load modal once first time loaded // cookie value still 'false' <% if @modal_loaded_once = false %> <script> $(window).load(function(){ $("a.modal:first").trigger("click"); }); </script> <% end %> edit: answer
$(window).load(function(){ if ($.cookie("show_modal_shownnx1") == null) { $.cookie('show_modal_shownnx1', 'yes', { expires: 720, path: '/' }); $("a.modal:first").trigger("click"); } });
Comments
Post a Comment