Reputation: 12010
I am using a lot of hashes in my URL for displaying messages and stuff, but I have a problem. It is fine when you click on a conversation to read it:
But, look what happens when the user attaches another hash to the end: (the subject disappears because the JavaScript gets confused)
(Click images to view larger versions)
How can I remove that second hash if there is one? I did this in Gmail before and it automatically removed them. So, how can I do this with my system? Here's my hash code:
if (window.location.hash) {
var messageID = window.location.hash.replace('#!/message/', '');
var msgSubject = $('#subject_' + messageID).text();
//the below code checks if conversation exists
$.get('tools.php?type=id_check&id=' + messageID, function(data) {
if (data == 'true') {
setTimeout(function() {
readMessage(messageID, msgSubject);
}, 200);
}
else {
alertBox('The requested conversation does not exist.', 2500);
window.location = '#';
}
});
}
Thanks in advance!
Upvotes: 0
Views: 109
Reputation: 4448
You can try
var matches = window.location.hash.match(/#!\/message\/(\d+)/);
if (matches) {
var messageId = matches[1];
// ...
}
This will capture a series of only digits after #!/message/
. If the hash contains #!/message/123
, then matches
will be an array
['#!/message/123', '123']
and so matches[1]
will contain the message id. If there is anything before or after it in the hash, it will be ignored. If there are no matches, matches
will be null
.
Upvotes: 2