Reputation: 111
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
Reputation: 3355
You can use a regex:
var hash = window.location.hash.match(/\/(\d)/)[1]
Upvotes: 1
Reputation: 66693
Try with split()
instead of substring:
var parts = window.location.hash.split('/');
var id = parts[parts.length - 1];
Upvotes: 0