Forum Moderators: open

Message Too Old, No Replies

Parse File Name from Current URL

Parse File Name from Current URL

         

Lingerboy

7:04 pm on Jul 22, 2003 (gmt 0)

10+ Year Member



Hi,

I need some code that will look at the URL of the page on which the code appears, and display the filename.

Example

I want this url....

www.mydomain.com/directory/filename.html

to return this value...

filename

Any suggestions would be appreciated.

bcolflesh

7:21 pm on Jul 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can this easily with various server-side languages. Does your account have PHP, ASP, etc access?

bcolflesh

7:25 pm on Jul 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just looked and saw an example for a client-side javascript:


var string="www.mydomain.com/directory/filename.html";
var getit= new Array();
getit=string.split("directory/");
alert(getit[1]);
var result=getit[1];

Lingerboy

8:17 pm on Jul 22, 2003 (gmt 0)

10+ Year Member



Hi bcolflesh,

That client side java looks like the ticket. I'm kind of new to this. What would be the full code and where should it go on the page (in the header, body, etc?). I want it to display the file name. Also, will this strip off the suffix (.html)?

Thanks!

bcolflesh

8:33 pm on Jul 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remember, this is JavaScript - not JAVA:


var string="www.mydomain.com/directory/filename.html";
var getit= new Array();
var getit=string.split("directory/");
var result=getit2[1];
var filename=result.split(".");
document.write(filename);

Put that in a file called filename.js and call it via:


<script type='text/javascript' src='http://www.mydomain.com/inc/filename.js'></script>

Lingerboy

9:29 pm on Jul 22, 2003 (gmt 0)

10+ Year Member



I am in your debt. I have one last questions....

I want the URL being parsed to be the URL of the page that the script is being called from....do you know if there is a variable I can use that will do that?

bcolflesh

9:52 pm on Jul 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change:


var string="www.mydomain.com/directory/filename.html";

to:


var string=(location.href);

gph

10:20 pm on Jul 22, 2003 (gmt 0)

10+ Year Member



You might find this of use. It does on and offline file paths.

function file_name_only(str) {
var slash = '/'
if (str.match(/\\/)) {
slash = '\\'
}
return str.substring(str.lastIndexOf(slash) + 1, str.lastIndexOf('.'))
}

alert(file_name_only(document.location.href))

gph

11:06 pm on Jul 22, 2003 (gmt 0)

10+ Year Member



Just thinking, the offline part will probably just be a waste of bytes.

For online you can use either of these 2 ways. Where str is your url.


function file_name_only(str) {
return str.substring(str.lastIndexOf('/') + 1, str.lastIndexOf('.'))
}

or

var myVar = str.substring(str.lastIndexOf('/') + 1, str.lastIndexOf('.'))


The function is only useful if you're using it more than once.

Lingerboy

12:48 am on Jul 23, 2003 (gmt 0)

10+ Year Member



bcolflesh,

Thanks so much for your help! After a bit of trial and error, I got it working. Here's my (your) code in it's final form...

var string=(location.href);
var getit=new Array();
var getit=string.split('directory/');
var result=getit[1];
var filename=result.split('.');
document.write("<a href='http://othersite/otherdirectory/")
document.write(filename[0])
document.write(".php'>click here to see ")
document.write(filename[0])
document.write(" at Othersite.com</a>");

Very cool....my 1st JavaScript!

Thanks again!

gph

4:45 am on Jul 23, 2003 (gmt 0)

10+ Year Member



I posted document.location.href thinking it was not always necessary but could be. I've used it in the past with multiple documents. Now I think it's one of those things that works but isn't correct.

The window object contains location and document. Assuming referencing the window isn't necessary, the only thing required in front of location.href would be a reference to another document.

gph

3:07 am on Jul 24, 2003 (gmt 0)

10+ Year Member



bcolflesh I'm confused by this line of code:

var getit=new Array();

Wouldn't the split() method create an array making that line unrequired? Maybe it's part of something else that didn't get removed in your post?

Lingerboy very nice to see someone enjoying learning. One of the beauties of JavaScript is that there is usually countless ways to come to the same answer. bcolflesh has shown you the use of the split() method. It makes an array from a string. What I posted uses other methods to extract part of a string.

If I'd known you didn't just need a snippet of code I would have written my posts in a way that made it understandable. My apologies for making that assumption. Here is a more readable version of the same thing.

var str = 'www.mydomain.com/directory/filename.html'
var number_1 = str.lastIndexOf('/') + 1
var number_2 = str.lastIndexOf('.')
var myVar = str.substring(number_1, number_2)
document.write(myVar)

Here is a break down starting with the string.

var str = 'www.mydomain.com/directory/filename.html'

The next line finds the numeric position in the string of the first forward slash searching from the end backwards in the string.

var number_1 = str.lastIndexOf('/') + 1

The + 1 at the end says we want the numeric position of the next character after the forward slash (we don't want the forward slash).

The next line does the same as number_1 but looks for a period

var number_2 = str.lastIndexOf('.')

The next line says myVar equals a substring of the original starting from number_1 and ending at number_2

var myVar = str.substring(number_1, number_2)

I'm pretty sure you'll find lots to learn here :) Click on the blue links for examples.

[devguru.com...]