Looks like the easy compact "built in" way is simply to add the "permalink" control with anchor:true like so:
map.addControl(new OpenLayers.Control.Permalink({anchor: true}));
That will make URLs with a particular parameter format appearing after the '#'. For example: #zoom=10&lat=51.48957&lon=-0.05879&layers=B
There's a nice short example on the open layers site or my example here shows it working with a layer picker.
But if you want to build this yourself (perhaps because you want to customise this URL format), then as others have explained, you need to
- register the 'moveend' event.
- In the called function,
construct your URL and set it with something like:
window.location.hash = zoom + '/' + lat + '/' + lon + '/' + layer;
- For this to work as a permalinkable URL, you'll also need something which reads the URL at start up, parses out the params after the '#' and sets the map location.
There's a full example on the OpenLayers3 site or I've made an example and done it slightly differently