0
    hash = hash.replace( /^#/, '' );
var node = $( '#' + hash );
if ( node.length ) {
  node.attr( 'id', '' );
}
document.location.hash = hash;
if ( node.length ) {
  node.attr( 'id', hash );
}

Im having trouble with the code above, which I grabbed from this article: Modifying document.location.hash without page scrolling, trying to temporarily replace the id of a div while I change the location.hash to said id. hash comes up with this error in the console: Uncaught ReferenceError: hash is not defined. I'm new to JS and need a little guidance! thanks :)

Community
  • 1
  • 1
Melzee
  • 91
  • 8

2 Answers2

1

The first line seems wrong:

hash = hash.replace( /^#/, '' );

Are you sure you defined the hash variable before it?

I think it has to be like this:

var hash = document.location.href;
hash = hash.replace( /^#/, '' );

and then the rest of code shouldn't cause the error

Kirill Ivlev
  • 12,310
  • 5
  • 27
  • 31
1
  • Make sure you've properly defined hash.

  • Make sure you also return false if you're handling the click event of an anchor.

Alex
  • 34,899
  • 5
  • 77
  • 90