/    Sign up×
Community /Pin to ProfileBookmark

I’m trying to use the indexof function to get part or the url below.

[url]http://www.site.co.uk/facilities/build.asp[/url]

[CODE]thisPage = location.href.substring((location.href.lastIndexOf(“/”))+1)
alert(thisPage); [/CODE]

I just want to get the word in between the two /…/ in this case facilities but I’m having problems.

Any help would great.

Thanks

Jemes

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Typhoon101Jul 24.2007 — You could try this.

[CODE]<script>
var theString = document.location.href;
var dir = theString.split("/");
alert(dir[dir.length-2]);
</script>[/CODE]


This will return the final directory name regardless of how many folders deep the file is.

i.e. [URL]http://mysite.com/directory-1/directory-2/directory-3/directory-4/file.asp[/URL] will return directory-4.

Hope this is what you want.
Copy linkTweet thisAlerts:
@jemesauthorJul 24.2007 — Perfect, thanks for your help.

Jemes
×

Success!

Help @jemes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...