0

Looking to populate a view based on part of a URL.

www.example.com/f/objectID I have a website with a view that loads based on the an objectID that is stored on parse.com

how can I extract/interpret "objectID" from the url to populate the /f/ view?

2
  • Do you want to get "objectID" string from "www.example.com/f/objectID" string? Commented Feb 8, 2013 at 2:20
  • Try to be more clear... What did you try? Commented Feb 8, 2013 at 2:37

2 Answers 2

3

Take a look at the window.location object. From there you just need some string methods.

var objectID = location.pathname.split('/').pop();

.split() will split the path into the fragments separated by / and .pop() will retrieve the last one. Of course, if objectID may not always be the last segment, use the proper (0-based) index to retrieve it from array returned by split.

Another solution that may have better performance (shouldn't really make a difference for this use case), assumes that objectID is the last segment:

var objectID = location.pathname.substring(location.pathname.lastIndexOf('/')+1);

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

A regex would work or splitting the window.location.url:

var objectId = window.location.pathname.replace(/\/f\/(\w+)\/?/, '$1');

OR

var objectId = window.location.pathname.split('/')[2];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.