Skip to content
Advertisement

Need simple modification for Javascript Cookie notice wordpress plugin

I followed a tutorial to create cookie notice popup for my wordpress site, it works as expected, I’m not good at javascript, so I want after user click on X button and accept cookies, to be automatically redirected to the same page with ?ok=3 query

<script>cookieLaw={dId:"cookie-law-div",bId:"cookie-law-button",iId:"cookie-law-item",show:function(e){if(localStorage.getItem(cookieLaw.iId))return!1;var o=document.createElement("div"),i=document.createElement("p"),t=document.createElement("button");i.innerHTML=e.msg,t.id=cookieLaw.bId,t.innerHTML=e.ok,o.id=cookieLaw.dId,o.appendChild(t),o.appendChild(i),document.body.insertBefore(o,document.body.lastChild),t.addEventListener("click",cookieLaw.hide,!1)},hide:function(){document.getElementById(cookieLaw.dId).outerHTML="",localStorage.setItem(cookieLaw.iId,"1")}},cookieLaw.show({msg:"We use cookies to give you the best possible experience. By continuing to visit our website, you agree to the use of cookies as described in our <a href='#'>Cookie Policy</a>",ok:"x"});</script>

Advertisement

Answer

Although I felt bad that some people instead of helping me fix my issue, they decide to give me negative rate. but I’ll share the solution which fix my issue, in case other people facing the same issue, the problem wasn’t in finding the JS code to navigate to a web page location.href, the problem was where should I add it in this minifyed code.

Anyways, the solution is:

adding ,location.href = window.location.href+"?ok=3";

after localStorage.setItem(cookieLaw.iId,"1"),

and before }},cookieLaw.show({

so the final code will be:

<script>cookieLaw={dId:"cookie-law-div",bId:"cookie-law-button",iId:"cookie-law-item",show:function(e){if(localStorage.getItem(cookieLaw.iId))return!1;var o=document.createElement("div"),i=document.createElement("p"),t=document.createElement("button");i.innerHTML=e.msg,t.id=cookieLaw.bId,t.innerHTML=e.ok,o.id=cookieLaw.dId,o.appendChild(t),o.appendChild(i),document.body.insertBefore(o,document.body.lastChild),t.addEventListener("click",cookieLaw.hide,!1)},hide:function(){document.getElementById(cookieLaw.dId).outerHTML="",localStorage.setItem(cookieLaw.iId,"1"),location.href = window.location.href+"?ok=3";}},cookieLaw.show({msg:"We use cookies to give you the best possible experience. By continuing to visit our website, you agree to the use of cookies as described in our <a href='#'>Cookie Policy</a>",ok:"x"});</script>

Maybe the solution was easy, but as I mentioned, I’m good at javascript, specially that the code looks minifyed.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement