取<a> tag value and set as cookie

时间:2016-05-17 03:34:22

标签: javascript php html

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?

1 个答案:

答案 0 :(得分:0)

假设id标记的<a>myUrl,那么您可以通过

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

您可以使用

设置Cookie
document.cookie = "href=" + myUrl;

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