user2128056
user2128056

Reputation: 111

Grabbing hash link from url

I have url as: /#profile/7 how to make it grab that last number from there? I'm looking for creating an mysql connection from that, SELECT something FROM somewhere WHERE id=':profile' Also, is it safe this way, is it even possible to make a connection? This is what I've got.

<script>
      var hash = window.location.hash.substring(6);
      alert (hash);
</script>

Upvotes: 0

Views: 77

Answers (2)

Jeff Shaver
Jeff Shaver

Reputation: 3355

You can use a regex:

var hash = window.location.hash.match(/\/(\d)/)[1]

Upvotes: 1

techfoobar
techfoobar

Reputation: 66693

Try with split() instead of substring:

var parts = window.location.hash.split('/');
var id = parts[parts.length - 1];

Upvotes: 0

Related Questions