用于捕获文件名的JavaScript [英] javascript for capturing filename

查看:58
本文介绍了用于捕获文件名的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个在实例上捕获文件名的问题

onclick在< body>中激活了

该函数应该捕获文件名并显示它。


第二个问题,我必须以相同的方式捕捉图像名称

using = onclick showimage(this)。这是否可以在

我尝试了所有组合

document.getElementbyId();

document.getElementbyName();

document.getElementbyTagName();

document.img [] .src();


请帮助

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

推荐答案

" Sharad Gupta" <克_ ****** @ yahoo.com>在留言中写道

news:7e ************************** @ posting.google.c om ...
"Sharad Gupta" <g_******@yahoo.com> wrote in message
news:7e**************************@posting.google.c om...
我有这个问题,当在< body>中激活
onclick时捕获实例上的文件名
该函数应该捕获文件名并显示它。

第二个问题,我必须以同样的方式捕捉图像名称
使用= onclick showimage(this)。这是否可以在
我已经尝试了
document.getElementbyId();
document.getElementbyName();
document.getElementbyTagName();
文件的所有组合.img [] .src();

请帮助
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




这会有帮助吗?请注意自动换行。


< html>

< head>

< title> show.htm< / title>

< script type =" text / javascript">

function showfile(){

var pathname = location。路径名;

var filename =

pathname.substr(pathname.lastIndexOf(" \\")+ 1,pathn ame.length);

alert(文件名);

}

函数showpath(){

var pathname = location.pathname;

pathname = pathname.substr(1,pathname.length);

alert(路径名);

}

功能showimage (那){

alert(that.name);

}

< / script>

< ; / head>

< body>

< input type =" button"值= QUOT;路径+文件" onclick =" showpath()">

< input type =" button" value ="仅文件 onclick =" showfile()">

< img src =" http://www.google.com/images/logo.gif" name =" Google"

onclick =" showimage(this)">

< / body>

< / html>



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>


哇,这解决了我的问题到gr8范围。


我想要的是脚本函数应该只选择

文件名的主要名称。示例..如果文件名是we1234.xyz

该函数应选择we1234并在警报时显示。


请帮助

***通过开发人员指南 http://www.developersdex.com 发送***

不要只是参加USENET ......获得奖励!
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!


> pathname.substr(pathname.lastIndexOf(" \\")+ 1,pathn ame.length);


这是多余的。

substr(startIndex [,endIndex])。

endIndex是可选的。

如果省略endIndex,则substr结束。


要摆脱你的延伸,试试这个。

filename = pathname.substr(pathname.lastIndexOf(" \\")+ 1,

pathname.lastIndexOf("。"));


我不记得如果substr包含lastIndex中的字符或

不是。如果是这样,从endIndex参数中减去一个。

filename = pathname.substr(pathname.lastIndexOf(" \\")+ 1,

pathname。 lastIndexOf("。") - 1);
> 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);


这篇关于用于捕获文件名的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆