469,312 Members | 2,437 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

javascript for capturing filename

i have this problem of capturing the filename on the instance when
onclick is activated in the <body>
the function should catch the filename and display it.

Second problem, i have to catch the image name in the same fashion
using =onclick showimage(this). Is this possible at at
I have tried all combinations of
document.getElementbyId();
document.getElementbyName();
document.getElementbyTagName();
document.img[].src();

pls help
Jul 23 '05 #1
12 15631
"Sharad Gupta" <g_******@yahoo.com> wrote in message
news:7e**************************@posting.google.c om...
i have this problem of capturing the filename on the instance when
onclick is activated in the <body>
the function should catch the filename and display it.

Second problem, i have to catch the image name in the same fashion
using =onclick showimage(this). Is this possible at at
I have tried all combinations of
document.getElementbyId();
document.getElementbyName();
document.getElementbyTagName();
document.img[].src();

pls help


Will this help? Watch for word-wrap.

<html>
<head>
<title>show.htm</title>
<script type="text/javascript">
function showfile() {
var pathname = location.pathname;
var filename =
pathname.substr(pathname.lastIndexOf("\\")+1,pathn ame.length);
alert(filename);
}
function showpath() {
var pathname = location.pathname;
pathname = pathname.substr(1,pathname.length);
alert(pathname);
}
function showimage(that) {
alert(that.name);
}
</script>
</head>
<body>
<input type="button" value="Path+File" onclick="showpath()">
<input type="button" value="File Only" onclick="showfile()">
<img src="http://www.google.com/images/logo.gif" name="Google"
onclick="showimage(this)">
</body>
</html>
Jul 23 '05 #2
Wow that solved my problem to the gr8 extent.

What I wanted exactly is that the script function should only pick the
primary name of the filename. Example.. if the file name is we1234.xyz
The function should pick we1234 and display on alert.

Pls help

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3
> pathname.substr(pathname.lastIndexOf("\\")+1,pathn ame.length);

This is redundant.
substr(startIndex [,endIndex]).
endIndex is optional.
If endIndex is omitted, the substr goes to the end.

To get rid of your extention try this.
filename = pathname.substr(pathname.lastIndexOf("\\")+1,
pathname.lastIndexOf("."));

I don't remember off hand if substr includes the character at lastIndex or
not. If so, subtract one from the endIndex argument.
filename = pathname.substr(pathname.lastIndexOf("\\")+1,
pathname.lastIndexOf(".")-1);
Jul 23 '05 #4
this has solved my problem. Either when we do alert or pass the result
to some div or span, it shows the primary name of the filename.

function showfile(that) {
var pathname = that.src;
var filename =
pathname.substr(pathname.lastIndexOf("/")+1,pathname.length);
document.getElementById("disp").innerText = filename;
Thanks for your help
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #5
Sharad Gupta <g_******@yahoo.com> wrote in message news:<41**********************@news.newsgroups.ws> ...
Wow that solved my problem to the gr8 extent.

What I wanted exactly is that the script function should only pick the
primary name of the filename. Example.. if the file name is we1234.xyz
The function should pick we1234 and display on alert.

Pls help

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

The prev ex. solved the problem, with some changes done.
But still I can have the filename as we1234.xyz & not as we1234, what
I want to achieve. Actually I want to give the primary part of
filename as the name of the picture, so that I donr have to put in the
picture name again n again for 1000's of them. So i found the result
thru

var filename = pathname.substr(pathname.lastIndexOf("/")+1,pathname.length);
document.getElementById("disp").innerText = filename;

this gives filename(primary.ext), but I want only primary name.
thanks and waiting for reply
Jul 23 '05 #6
Sharad Gupta wrote:

[snip]

var filename = pathname.substr(pathname.lastIndexOf("/")+1,pathname.length);
document.getElementById("disp").innerText = filename;

this gives filename(primary.ext), but I want only primary name.
thanks and waiting for reply


var filename = pathname.split(".");
document.getElementById("disp").innerHTML = filename[filename.length-1]
Mick
Jul 23 '05 #7
hi

thru the code i am able to generate the filename extension and not the
primary name of the filename.

ie we1234.xyz

i am getting xyz, where as i want we1234

still waiting
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #8
Sharad Gupta <g_******@yahoo.com> wrote:
thru the code i am able to generate the filename extension and not the
primary name of the filename.
ie we1234.xyz
i am getting xyz, where as i want we1234


function showfile(that) {
var pathname = that.src;
var filename =
pathname.substr(pathname.lastIndexOf("/")+1,pathname.length);
var filename = filename.substr(filename,filename.lastIndexOf(".") );
document.getElementById("disp").innerText = filename;
}

works with "lulu.lili_lolo.jpg"
.................^
--
@@@@@
E -00 comme on est very beaux dis !
' `) /
|\_ =="
Jul 23 '05 #9
Sharad Gupta wrote:

ie we1234.xyz

i am getting xyz, where as i want we1234

still waiting


var filename = pathname.split(".");
document.getElementById("disp").innerHTML = filename[filename.length-2]

"length-2"
Mick
Jul 23 '05 #10
Sharad Gupta wrote

ie we1234.xyz

i am getting xyz, where as i want we1234

var filename = pathname.split("/");
document.getElementById("disp").innerHTML =
filename[filename.length-1].split(".")[0]
Disregard earlier answer.
Mick
Jul 23 '05 #11
"Sharad Gupta" <g_******@yahoo.com> wrote in message
news:41**********************@news.newsgroups.ws.. .
hi

thru the code i am able to generate the filename extension and not the
primary name of the filename.

ie we1234.xyz

i am getting xyz, where as i want we1234

still waiting


Were you waiting for me to respond?

Here's a variation of what I posted before.

Watch for word-wrap.

<html>
<head>
<title>show.htm</title>
<script type="text/javascript">
function showfile() {
var pathname = location.pathname;
var filename =
pathname.substr(pathname.lastIndexOf("\\")+1,pathn ame.length);
alert(filename);
}
function showpath() {
var pathname = location.pathname;
pathname = pathname.substr(1,pathname.length);
alert(pathname);
}
function showpref() {
var pathname = location.pathname;
var filename =
pathname.substr(pathname.lastIndexOf("\\")+1,pathn ame.length);
filename = filename.substr(0,filename.indexOf("."))
alert(filename);
}
function showimage(that) {
alert(that.name);
}
</script>
</head>
<body>
<input type="button" value="Path+File" onclick="showpath()">
<input type="button" value="File Only" onclick="showfile()">
<input type="button" value="File Pref" onclick="showpref()">
<img src="http://www.google.com/images/logo.gif" name="Google"
onclick="showimage(this)">
</body>
</html>
Jul 23 '05 #12
Thanks a lot..
it really solved my problem... other than this i also choked a function
with help of subStr and str.length.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Steve Wolfie | last post: by
2 posts views Thread by Ken Cox - Microsoft MVP | last post: by
2 posts views Thread by sergio | last post: by
8 posts views Thread by chrisdude911 | last post: by
19 posts views Thread by Prisoner at War | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.