javascript - set a cookie clicking a menu link in wordpress -
i need register cookie when specific menu link clicked (i utilize cookie redirect mobile user mobile version of site desktop version). in non wp site utilize onclick
attribute accomplish this, in example:
<script> function setupcookie() { document.cookie = "desktop=true;domain=.mysite.com"; } </script> <a href="mysite.com" onclick="setupcookie();"> desktop version </a>
but i'm having problem doing in wordpress due how menus handled, html construction (i added class desktop-redirect
using custom css wp menu field).
<li class="desktop-redirect menu-item menu-item-type-custom menu-item-object-custom menu-item-1261"> <a href="mysite.com">desktop version</a> </li>
i've tried this:
function setupcookie() { document.cookie = "desktop=true;domain=.mysite.com"; } element.addeventlistener("click", setupcookie(),false); var element = document.getelementsbyclassname("desktop-redirect")[0].getelementsbytagname("a")[0];
but doesn't work (the desktop-redirect
class unique li
element).
thanks in advance.
try this:
function setupcookie() { document.cookie = "desktop=true;domain=.mysite.com"; } document.getelementsbyclassname("desktop-redirect")[0].getelementsbytagname("a")[0].addeventlistener("click", setupcookie);
note getelementsbyclassname did not capitalize "n" in name... not sure if code, or typo, 1 issue. also, leave out () in function call.
javascript wordpress cookies
No comments:
Post a Comment