Updating window.location.hash if target is not jQM element that uses it

This is the tested and optimized version of the code posted in #980.