# Getting document.URL

**URL:** <https://talk.observablehq.com/t/getting-document-url/2110>\
**Category:** Help\
**Created:** [July 2, 2019, 1:14pm UTC](https://talk.observablehq.com/t/getting-document-url/2110 "2019-07-02T13:14:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nitaku](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nitaku/32/92_2.png) [@nitaku](https://talk.observablehq.com/u/nitaku)\
**Post date:** [July 2, 2019, 1:14pm UTC](https://talk.observablehq.com/t/getting-document-url/2110/1 "2019-07-02T13:14:09Z")

</div>

Is there a way to get the value of `document.URL` from a notebook? I am getting a value that’s different from the one displayed in the browser’s address bar, as can be seen in this notebook:

> **[URL](https://observablehq.com/@nitaku/url)**
>
> An Observable notebook by Matteo Abrate.

Similar problems are encountered also with `location.href` or `location`.

My intent was to read the value of `location.hash` into a cell value.

---

<div class="post-metadata">

**Author:** ![bgchen](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bgchen/32/784_2.png) [@bgchen](https://talk.observablehq.com/u/bgchen)\
**Post date:** [July 2, 2019, 3:03pm UTC](https://talk.observablehq.com/t/getting-document-url/2110/2 "2019-07-02T15:03:21Z")

</div>

`document.URL` and the other approaches you’re using will fail because Observable notebooks run in an iframe. ~~I don’t see an easy way to read the URL of the containing page since that is likely to go against the [same-origin policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy). E.g. `window.parent.location.href` returns `SecurityError: Permission denied to get property "href" on cross-origin object`.~~ Actually, see below!

**Edit** : Oh, I just realized that you said you only want the `location.hash`. If you’re planning to parse that info and use it as user-provided data in your notebook, an alternate approach is to use the query parameters (stuff after `?` in the URL) as shown in these notebooks:

> **[Parameterized Notebooks](https://observablehq.com/@mbostock/parameterized-notebooks)**
>
> An Observable notebook by Mike Bostock.

> **[Permalink](https://observablehq.com/@jeffbaumes/permalink)**
>
> An Observable notebook by Jeff Baumes.

**EDIT 2** : Hmm, the notebooks above provide a solution to part of the original question: `document.baseURI` will return the URL of the containing page, but unfortunately it does not seem to contain the URL hash…

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [July 2, 2019, 4:45pm UTC](https://talk.observablehq.com/t/getting-document-url/2110/3 "2019-07-02T16:45:33Z")

</div>

This should work:

```auto
Object.assign(document.createElement("a"), {href: ""}).hash

```

Edit: actually it won’t because we don’t pass the hash down to the worker. So, you’ll need to use the query string, at least for now.

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [July 2, 2019, 6:11pm UTC](https://talk.observablehq.com/t/getting-document-url/2110/4 "2019-07-02T18:11:33Z")

</div>

Just for reference, the shortest way (I know of) to obtain the notebook URL would be:

```javascript
html`<a href>`.href

```

---

<div class="post-metadata">

**Author:** ![nitaku](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nitaku/32/92_2.png) [@nitaku](https://talk.observablehq.com/u/nitaku)\
**Post date:** [July 3, 2019, 1:54pm UTC](https://talk.observablehq.com/t/getting-document-url/2110/5 "2019-07-03T13:54:08Z")

</div>

Thank you all for your quick and thorough help 🙂

So until the hash is made visible to the worker, there is no solution.

I’ll go with query strings for now, then I’ll switch to hash URIs when exporting the code outside Observable.

Thanks again, and keep up the great work!

---

<div class="post-metadata">

**Author:** ![jashkenas](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/jashkenas/32/1778_2.png) [@jashkenas](https://talk.observablehq.com/u/jashkenas)\
**Post date:** [July 11, 2019, 7:07pm UTC](https://talk.observablehq.com/t/getting-document-url/2110/6 "2019-07-11T19:07:40Z")

</div>

Thanks for the request!

We now pass down query strings and hash parts of the URL from the parent to the worker, so you should be able to do this.

Here’s a quick demonstration notebook:

> **[URL querystrings and hash parameters](https://observablehq.com/@jashkenas/url-querystrings-and-hash-parameters?a=b#foo)**
>
> An Observable notebook by Jeremy Ashkenas.

Good luck!

---

<div class="post-metadata">

**Author:** ![nitaku](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nitaku/32/92_2.png) [@nitaku](https://talk.observablehq.com/u/nitaku)\
**Post date:** [October 7, 2019, 10:17am UTC](https://talk.observablehq.com/t/getting-document-url/2110/7 "2019-10-07T10:17:43Z")

</div>

Thank you for the solution! Sorry for coming back to you this late ^\_^’

This community is fantastic btw. 😃
