set-location-hash v0.3.0
set-location-hash.js
A client-side library to set the anchor portion of current URL without scrolling
// On http://yoursite.com/
setLocationHash('foo');
location.href; //=> "http://yoursite.com/#foo"Installation
Package managers
Bower
bower i --save set-location-hashnpm
npm i --save set-location-hashDuo
var setLocationHash = require('shinnn/set-location-hash.js');Standalone
Download the script file directly.
AMD support
This repository includes the AMD-friendly build but the package managers doesn't include it. If you want to use it, download it directly.
API
setLocationHash(identifier , options)
identifier: String
options: Object
Return: String (Current entire URL)
Change the fragment identifier of current URL to the String of the identifier argument.
This function is vary similar to location.hash property, but this function won't change the scroll position of the page unlike location.hash.
If the browser supports history.pushState and history.replaceState, you can set replace and force options by passing an Object to the options argument.
options.replace
Type: Boolean
Default: false
Replace the browser history instead of pushing a new history.
// On http://yoursite.com/
history.length; //=> 1
setLocationHash('foo', {replace: true});
history.length; //=> 1
setLocationHash('bar');
history.length; //=> 2options.force
Type: Boolean
Default: false
By default, this function doesn't push a new history when the old URL is the same as new one. If you set this option true, it will push a new history in any case.
// On http://yoursite.com/#foo
history.length; //=> 1
setLocationHash('foo');
history.length; //=> 1
setLocationHash('foo', {force: true});
history.length; //=> 2License
Copyright (c) 2014 Shinnosuke Watanabe
Licensed under the MIT License.
