如何用 JavaScript 获取当前 URL 的文件名?

neodotnet 2004-09-11 03:26:31
要求不含扩展名

比如当前页面 URL 为
http://community.csdn.net/Expert/PostNew.asp
得到的结果是
PostNew

就是获得最后一个 ‘/’ 和最后一个 ‘.’ 间的字符串,如果没有文件名,即以 ‘/’ 结尾的 URL 则返回空字符串

如何实现?
...全文
1336 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
neodotnet 2004-09-11
  • 打赏
  • 举报
回复
我按照下面的方法实现了

var docUrl = document.URL;
var lastPipe = docUrl.lastIndexOf('/');
var lastPeriod = docUrl.lastIndexOf('.');
var fileName;

if (lastPipe < lastPeriod)
fileName = docUrl.substring(lastPipe + 1, lastPeriod);
else
fileName = '';
ecobin 2004-09-11
  • 打赏
  • 举报
回复
学习学习再学习。。lastIndexOf()

mjpclab 2004-09-11
  • 打赏
  • 举报
回复
要是这样的话只能在服务器端想办法
neodotnet 2004-09-11
  • 打赏
  • 举报
回复
我想我可以搞定他了,我再试试
neodotnet 2004-09-11
  • 打赏
  • 举报
回复
是不是有 lastIndexOf() 这个方法, 如果有, 那就好办多了
mjpclab 2004-09-11
  • 打赏
  • 举报
回复
<script language=javascript>
var fname=location.pathname;
fname=fname.substr(fname.lastIndexOf('/')+1);
alert(fname);
</script>
neodotnet 2004-09-11
  • 打赏
  • 举报
回复
楼上的方法是对的,不过, 如果 URL 是下面的, 我不知道会出现什么不可预料的结果

http://community.csdn.net/
zltostem 2004-09-11
  • 打赏
  • 举报
回复
<script language="javascript">
<!--
window.onload = function()
{
alert(document.URL);//or document.URLUnencoded
}
//-->
</script>
ecobin 2004-09-11
  • 打赏
  • 举报
回复
笨方法

<script type="text/javascript">
<!--
var url = "http://community.csdn.net/Expert/PostNew.asp"
//var url = window.location.href
fileName = url.split("//")[1].split("/")
file = fileName[fileName.length-1].split(".")[0]
document.write(file)
-->
</script>
neodotnet 2004-09-11
  • 打赏
  • 举报
回复
小弟不才, 没看出上面哪个函数是我需要的, 还请哪位大哥指点

我现在想获取一个 URL 中最后一个 "/" 字符的位置,有没有现成的函数? 如没有是不是得用 indexOf() 方法循环遍历到字符串的结尾? 直到找不到为止?
guoweidong 2004-09-11
  • 打赏
  • 举报
回复
我也是在asp高级编程3里突然看到的.
guoweidong 2004-09-11
  • 打赏
  • 举报
回复
呵呵,没注意吧,错误页里就有这样的代码.
guoweidong 2004-09-11
  • 打赏
  • 举报
回复
<META NAME="MS.LOCALE" CONTENT="ZH-CN">
</head>

<script>
function Homepage(){
<!--
// in real bits, urls get returned to our script like this:
// res://shdocvw.dll/http_404.htm#http://www.DocURL.com/bar.htm

//For testing use DocURL = "res://shdocvw.dll/http_404.htm#https://www.microsoft.com/bar.htm"
DocURL=document.URL;

//this is where the http or https will be, as found by searching for :// but skipping the res://
protocolIndex=DocURL.indexOf("://",4);

//this finds the ending slash for the domain server
serverIndex=DocURL.indexOf("/",protocolIndex + 3);

//for the href, we need a valid URL to the domain. We search for the # symbol to find the begining
//of the true URL, and add 1 to skip it - this is the BeginURL value. We use serverIndex as the end marker.
//urlresult=DocURL.substring(protocolIndex - 4,serverIndex);
BeginURL=DocURL.indexOf("#",1) + 1;
urlresult=DocURL.substring(BeginURL,serverIndex);

//for display, we need to skip after http://, and go to the next slash
displayresult=DocURL.substring(protocolIndex + 3 ,serverIndex);
InsertElementAnchor(urlresult, displayresult);
}

function HtmlEncode(text)
{
return text.replace(/&/g, '&').replace(/'/g, '"').replace(/</g, '<').replace(/>/g, '>');
}

function TagAttrib(name, value)
{
return ' '+name+'="'+HtmlEncode(value)+'"';
}

function PrintTag(tagName, needCloseTag, attrib, inner){
document.write( '<' + tagName + attrib + '>' + HtmlEncode(inner) );
if (needCloseTag) document.write( '</' + tagName +'>' );
}

function URI(href)
{
IEVer = window.navigator.appVersion;
IEVer = IEVer.substr( IEVer.indexOf('MSIE') + 5, 3 );

return (IEVer.charAt(1)=='.' && IEVer >= '5.5') ?
encodeURI(href) :
escape(href).replace(/%3A/g, ':').replace(/%3B/g, ';');
}

function InsertElementAnchor(href, text)
{
PrintTag('A', true, TagAttrib('HREF', URI(href)), text);
}

//-->
</script>

<body bgcolor="FFFFFF">

<table width="410" cellpadding="3" cellspacing="5">

<tr>
<td align="left" valign="middle" width="360">
<h1 style="COLOR:000000; FONT: 12pt/15pt 宋体"><!--Problem-->您没有权限查看该网页</h1>
</td>
</tr>

<tr>
<td width="400" colspan="2"> <font style="COLOR:000000; FONT: 9pt/11pt 宋体">您提供的证书可能没有权限查看该目录或网页。</font></td>
</tr>

<tr>
<td width="400" colspan="2"> <font style="COLOR:000000; FONT: 9pt/11pt 宋体">

<hr color="#C0C0C0" noshade>

<p>请尝试下列操作:</p>

<ul>
<li>单击<a href="javascript:location.reload()">刷新</a>按钮,并使用其他证书重试。</li>

<li>如果您确信应该能够查看该目录或网页,请与 Web 站点管理员联系,其电子邮件地址或电话号码请参阅 <script>
<!--
if (!((window.navigator.userAgent.indexOf("MSIE") > 0) && (window.navigator.appVersion.charAt(0) == "2")))
{
Homepage();
}
//-->
</script> 主页。</li>
</ul>

<h2 style="font:9pt/11pt 宋体; color:000000">HTTP 401.4 - 未授权:授权被筛选器拒绝<br> Internet 信息服务</h2>

<hr color="#C0C0C0" noshade>

<p>技术信息(支持个人)</p>

<ul>
<p>
<li>背景:<br>Web 服务器安装了筛选器程序以便检查连接到服务器的用户。该筛选器程序禁止通过连接到服务器的身份验证来访问资源。</p>

<p>
<li>详细信息:<br><a href="http://www.microsoft.com/ContentRedirect.asp?prd=iis&sbp=&pver=5.0&pid=&ID=401.4&cat=web&os=&over=&hrd=&Opt1=&Opt2=&Opt3=" target="_blank">Microsoft 支持</a>
</li></p>
</ul>

</font></td>
</tr>

</table>
</body>
</html>
当前的web页面保存为一个包含所有外部依赖项(样式、脚本、图像)的文件。 将一个完整的网页以当前状态保存为一个文件,并且所有内在依赖项(脚本,样式表,图像)都内嵌到该页面中。 如果您要归档页面,在任何设备上测试网站或出于SEO用途都非常有用。计算页面足迹也很有用:首次访问者必须下载多少数据? 与流行的Javascript框架(如JQuery,Modernizr,AngularJS,Knockout,Bootstrap等)完美配合使用。请注意,尽管内联页面仍可以连接到JSON或XML Web服务,但内联页面不会保留安全信息或会话,因此您可能会遇到身份验证问题。 版本1.4.1 *固定: <link>如果不是全部小写,则跳过标签1.4.0版*大大提高了速度*解决了内联永无止境的问题*解决了adblockers干扰内联的问题*添加了对图像的支持 <embed>和 标签版本1.3.0 *添加:带有进度信息和按钮的叠加,以停止内联并立即获取页面*添加:单击Chrome菜单图标时,用户获得使用右键菜单1.2.2版本的说明*固定:即使页面已正常保存,超时仍会触发*修复:下载的文件名中包含非法字符*修复:现在修复了一些下载资源问题1.2.1版*保存了内联页面时从文件名中删除了无效字符1.2版*重大重写为了提高速度*添加了选项页*修复了防止某些网页被内联的版本1.1.5 *更名为页面保护程序1.1.4的问题*修复了:不带src属性的img标签导致内联过程失败1.1.3版*修复:样式表中的@import语句未处理*修复:内联样式表中的url()和@import语句未处理*修复:页面输出使用错误的字符编码Ve保存 rsion 1.1.2 *修复:无法下载任何资源时阻止了进程暂停1.1.1版本*修复了:内联包含某些字符CSS中的url()引用未内嵌版本1.1 *修复了:CSS中的url()引用未内联*固定:未保存没有外部资源的页面*已添加:带有源URL和时间戳的元标记*已添加:内联资源时的活动指示*已更改:源URL用于创建文件名版本1.0 *初始发行版 </embed> 支持语言:English

87,909

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧