location-hash-stream

duplex streaming interface to the location hash

Usage no npm install needed!

<script type="module">
  import locationHashStream from 'https://cdn.skypack.dev/location-hash-stream';
</script>

README

location-hash-stream

Build Status npm install npm version js-standard-style License

streaming interface to the window location hash

example

Emits the location hash whenever it changes. Uses the hashchange event under the hood, so browser support is dictated therein.

const hashStream = require('location-hash-stream')

const stream = hashStream()

hashStream.on('data', data => console.log(data))

window.location.hash = 'lol'

// logs '#lol'

Writing to the stream sets the location hash.

const hashStream = require('location-hash-stream')

const stream = hashStream()

hashStream.write('hash')

// window.location.hash is now '#hash'

API

hashStream([opts]) -> stream

  • opts is an optional configuration object. Available options are:
    • stripHash a Boolean specifying whether the emitted location hash should be stripped of its leading # character. Defaults to false.
  • stream.getHash() -> Retrieve the current hash string, respects the stripHash parameter.

license

MIT