[GopherJS] Access href Value of Anchor <a> Tag


In JavaScript, the href value of anchor element/tag (<a>) can be accessed by:

// assume your website is:
//   https://example.com/
// assume the a tag is:
//   <a id="atag" href="/about/">About</a>

var elm = document.getElementById("atag");
// return "/about/"
var href = elm.getAttribute("href");
// return "https://example.com/about/"
var href = elm.href;

In GopherJS, the equivalent is:

elm := js.Global.Get("document")..Call("getElementById", "atag")
// return "/about/"
href := elm.Call("getAttribute", "href").String()
// return "https://example.com/about/"
href := elm.Get("href").String()

Tested on:

  • Ubuntu Linux 16.10
  • Go 1.7.4
  • Chromium Version 55.0.2883.87 Built on Ubuntu , running on Ubuntu 16.10 (64-bit)

References:

[1]GopherJS - A compiler from Go to JavaScript (GitHub, GopherJS Playground, godoc)
[2]

javascript access href - Google search

javascript access href - DuckDuckGo search

javascript access href - Bing search

javascript access href - Yahoo search

javascript access href - Baidu search

javascript access href - Yandex search

[3]anchor - How to get "raw" href contents in JavaScript - Stack Overflow