views:

57

answers:

2

Hello,

Here's what I want to happen

Anything the variable hash contains: #/projects/X

X being anything other than blank, I want to fire an alert

Fires Alerts

#/projects/X
#/projects/X/boo/123/123/ads/asd/ads/ads

Does not fire an alert

#/projects/

Suggestions?

A: 

You could replace the #/projects/ part, and see if there's anything left. If so, do something:

var hash = window.location.hash;

if (hash.indexOf('#/projects')!=-1) {
  if (hash.replace('#/projects/','').length > 0) {
    // do stuff
  }
}
Alec
Actually wait that doesn't work... because for other HASHS like #/contacts/ it would return true which is bad. It should only work for #/projects/ does that make sense?
AnApprentice
See edit; you can first check if "#/projects" exists.
Alec
+2  A: 
if (/^#\/projects\/.+/.test(window.location.hash)) {
    alert("Some project");
}
Tim Down
I want to know when it's #/projects/XXXXX meaning X is anything. So should it really be != 0? What is indexOf supposed to do here?
AnApprentice
Sorry, I read the question the wrong way round. Fixed.
Tim Down
Aaaah. So X is variable. Rewriting....
Tim Down
@tim ya thanks. I should have been clearer.
AnApprentice
..... and done.
Tim Down
gorgeous - thanks
AnApprentice