-1

So say we have an a tag in the page

<a href="www.url1.com">url1</a>

How can I set my cookie to www.url1.com with javascript onclick?

Ian
  • 30,182
  • 19
  • 69
  • 107
Rob
  • 631
  • 1
  • 7
  • 20
  • 1
    What specifically are you having trouble with? That's what SO would be for. A Google search and a tutorial are probably what you need. – 4castle May 17 '16 at 03:36
  • I dont know how to locate the tag and activate a function – Rob May 17 '16 at 03:38
  • http://stackoverflow.com/questions/29804219/set-a-cookie-based-on-url-parameter This isn't an answer to your question directly, but it's a good starting point to develop your script – Aaron Lavers May 17 '16 at 03:41
  • A tutorial is definitely what you need, such as this [JavaScript DOM Tutorial](http://www.w3schools.com/js/js_htmldom.asp). And this [JavaScript Cookies Tutorial](http://www.w3schools.com/js/js_cookies.asp) – 4castle May 17 '16 at 03:41
  • close aaron, but the src is onclick – Rob May 17 '16 at 03:51

1 Answers1

0

Say the id of your <a> tag is myUrl, then you can get the href value by

var myUrl = document.getElementById('myUrl').getAttribute("href");

You can set the cookie using

document.cookie = "href=" + myUrl;

JSFiddle : https://jsfiddle.net/quttmdx5/

Munawir
  • 3,346
  • 9
  • 33
  • 51