[GopherJS] window.location - Access URL


Access URL, path, query string, etc. of current URL via window.location and GopherJS.

import "github.com/gopherjs/gopherjs/js"

var location = js.Global.Get("location")

func accessURL() {
      // current URL: http://localhost:8000/code/gopherjs/window-location/index.html?a=1

      // return - http://localhost:8000/code/gopherjs/window-location/index.html?a=1
      location.Get("href").String()
      // return - localhost:8000
      location.Get("host").String()
      // return - localhost
      location.Get("hostname").String()
      // return - /code/gopherjs/window-location/index.html
      location.Get("pathname").String()
      // return - http:
      location.Get("protocol").String()
      // return - http://localhost:8000
      location.Get("origin").String()
      // return - 8000
      location.Get("port").String()
      // return - ?a=1
      location.Get("search").String()
}

For JavaScript equivalent, see [2].


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] window.location example - access browser url