/    Sign up×
Community /Pin to ProfileBookmark

I need to split a URL and store the last half in a variable for example [url]www.site/folder/index.htm[/url] to read /folder/index.htm. The length of the last half or the first is not always the same.

Any help here would be greatly appreciated.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@scragarMay 04.2005 — <script type="text/javascript"><!--
var fullURL = window.self.location.href;
var splitURL = fullURL.replace(/^(http://)?(www.)?/, "").split("/");
var tmpURL = "";
for(var i = 1; i < splitURL.length; i++){
tmpURL += splitURL[i]+"/";
};
tmpURL = tmpURL.replace(//$/, "");
alert("your folder location is:"+tmpURL);
//-->
</script>
Copy linkTweet thisAlerts:
@nzakasMay 04.2005 — What you actually want to use is this:

[CODE]var sPathName = document.location.pathname;[/CODE]

This gets you everything from the first / to the end of the URL.
Copy linkTweet thisAlerts:
@CharlesMay 04.2005 — <script type="text/javascript">
<!--
function URL (s) {
/^((w+:)/*)?((w+.)?w+.w+)(:(d+))?(/[^?#]+)?(?[^#]+)?(#.+)?/.exec (s);

<i> </i>this.protocol = RegExp.$2 || 'http:';
<i> </i>this.host = RegExp.$5 ? RegExp.$3 + ':' + RegExp.$6 : RegExp.$5;
<i> </i>this.hostname = RegExp.$3;
<i> </i>this.port = RegExp.$6;
<i> </i>this.pathname = RegExp.$7;
<i> </i>this.search = RegExp.$8;
<i> </i>this.hash = RegExp.$9;

<i> </i>this.href= RegExp.$2 ? s : this.protocol + '//' + s;
<i> </i>if (!this.pathname &amp;&amp; !//$/.test (this.href)) this.href = this.href + '/';

}

url = new URL ('www.site/folder/index.htm');
alert (url.pathname);
// --&gt;
&lt;/script&gt;
×

Success!

Help @Bryan_Tuffin 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 5.25,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...