通过onClick目标URL进入cookie

时间:2018-10-24 02:30:40

标签: javascript var

任何人都知道使用javascript将onClick目标URL传递到javascript var中的最佳方法是什么,然后我可以在其中写入会话cookie?

因此,当用户单击[Button 1]且目标为https://www.w3schools.com/js/时,理想情况下它将通过www.w3schools.com/js/

我尝试过的:

<script>  
 var destinationURL = window.location.href
 sessionStorage.setItem('destination', 'destinationURL');
 var destination = "destination"; // Name of your cookie
 var destinationURL = destinationURL; // Value of your cookie
 var expirationTime = 2592000; // One month in seconds
 expirationTime = expirationTime * 1000; // Converts expirationtime to milliseconds
 var date = new Date(); 
 var dateTimeNow = date.getTime(); 
 date.setTime(dateTimeNow + expirationTime); // Sets expiration time (Time now + one month)
 var expirationTime = date.toUTCString(); // Converts milliseconds to UTC time string
 document.cookie = cookieName+"="+cookieValue+"; expires="+expirationTime+"; path=/; domain=." + location.hostname.replace(/^www\./i, ""); // Sets cookie for all subdomains
</script>  

0 个答案:

没有答案