Welcome to Delicate template
Header
Just another WordPress site
Header

用js获取当前网址的文件名

6月 9th, 2011 | Posted by 无 名 in js

使用js获取当前地址栏中网址的文件名。
js代码如下:

<script type="text/javascript">
<!--
var url = window.location.href;
url = url.substring(url.lastIndexOf('/') + 1);//获取到文件名(有后缀名)
end = url.indexOf(".");//“.”所在的位置
url = url.substring(0,end);//进行截取字符
alert(url);//输出文件名(没有后缀名的)
-->
</script>

更简洁的代码如下:

<script type="text/javascript">
<!--
var url = window.location.href;
url=url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".")); 
alert(url);
-->
</script>

You can follow any responses to this entry through the RSS 2.0 Both comments and pings are currently closed.