Get filename from URL using Javascript
http://befused.com/javascript/get-filename-url
Get filename from URL using Javascript
This snippet will get the filename from the url. The filename is the last part of the URL from the last trailing slash. For example, if the URL is http://www.example.com/dir/file.html then
file.html is the file name.
Explanation
- var url = window.location.pathname;
This declares the url variable and adds the current pathname as its value.
- var filename = url.substring(url.lastIndexOf('/')+1);
- alert(filename);
substring (method) - extract characters from start (parameter).
url is the stringObject url.substring(start)
lastIndexOf (method) - position of last occurrence of specified string value, in this case the '/'
Add one to lastIndexOf because we do not want to return the '/'
Full snippet
- var url = window.location.pathname;
- var filename = url.substring(url.lastIndexOf('/')+1);
- alert(filename);
Other question:
https://stackoverflow.com/questions/423376/how-to-get-the-file-name-from-a-full-path-using-javascript
https://stackoverflow.com/questions/680929/how-to-extract-extension-from-filename-string-in-javascript
Get filename from URL using Javascript的更多相关文章
- 【转】关于URL编码/javascript/js url 编码/url的三个js编码函数
来源:http://www.cnblogs.com/huzi007/p/4174519.html 关于URL编码/javascript/js url 编码/url的三个js编码函数escape(),e ...
- 关于URL编码/javascript/js url 编码/url的三个js编码函数
关于URL编码/javascript/js url 编码/url的三个js编码函数escape(),encodeURI(),encodeURIComponent() 本文为您讲述关于js(javasc ...
- 关于URL编码/javascript/js url 编码
一.问题的由来 URL就是网址,只要上网,就一定会用到. 一般来说,URL只能使用英文字母.阿拉伯数字和某些标点符号,不能使用其他文字和符号.比如,世界上有英文字母的网址 “http://www.ab ...
- javascript获取当前url
在WEB开发中,许多开发者都比较喜欢使用javascript来获取当前url网址,本文就此为大家总结一下比较常用获取URL的javascript实现代码,以下示例是前面为相应实现方法,后面是获取URL ...
- JavaScript URL传值过程中遇到的问题及知识点总结
JavaScript URL传值过程中遇到的问题及知识点总结 Web系统开发过程中经常用到URL进行传值,刚刚接触时不太会解析,会出现中文乱码问题等. 1.父子页面之间的传值(在一个页面中以加载ifr ...
- 超实用的 JavaScript 代码片段( ES6+ 编写)
Array 数组 Array concatenation (数组拼接) 使用 Array.concat() ,通过在 args 中附加任何数组 和/或 值来拼接一个数组. const ArrayCon ...
- 关于在SharePoint 2013(2010)中Javascript如何实现批量批准的自定义操作功能?
1.概述: SharePoint 2013(包括SharePoint 2010)提供了很方便的,多选的界面,但是很多操作还是不能批量进行,比如:批准的功能.如果您要解决方案不关心代码,那么请直接联系作 ...
- 通过url 下载文件
1.问题简介 通过文件的url,将文件下载到本地.文件存储的位置为:tomcat服务器的文件夹(通过读取properties文件:可看:http://www.cnblogs.com/0201zcr/p ...
- 8年javascript知识点积累
08年毕业就开始接触javascript,当时是做asp.net发现很多功能用asp.net控件解决不了,比如checkbox单选,全选问题,自动计算总价问题,刷新问题,等等.那时感觉javascri ...
随机推荐
- Excel中mod函数的使用方法
1.mod函数的含义 1 1.mod函数是一个用来求余数函数,返回两数相除的余数.mod函数在Excel中一般不单独使用,经常和其他函数组合起来使用. END 2.mod函数的语法格式 1 2.mod ...
- php 抛出异常信息try catch
<meta charset="utf-8"> <?php /** * 自定义方法输出异常信息 */ $i=11; try { if ($i==1) { echo ...
- 左侧多级菜单,高亮显示js
左侧多级菜单,如果本页面是当前栏目,则左侧菜单高亮显示 <ul class="nav navbar-stacked" id="navs"> {ded ...
- Arduino IDE for ESP8266 项目(3)创建AP+STA
官网API:http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html STA (客户端)手机连接路由器 S1 *简 ...
- 查询rman 备份信息集
SELECT TRIM(START_TIME||'#'), TRIM(END_TIME||'#'), TRIM(CASE OUTPUT_DEVICE_TYPE ...
- SQL优化思路大全
一.百万级数据库优化方案 1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断 ...
- Qt Creator中根据为Windows系统还是Linux系统对源码进行条件编译
方法1: 在.h和.cpp文件中,针对需要不同平台编译的代码:添加上如下的条件编译指令: #ifdef 标识符 程序段1 #else 程序段2 #endif 举例说明如下: //Windows系统包含 ...
- PAT A1141 PAT Ranking of Institutions (25 分)——排序,结构体初始化
After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...
- 【Codeforces 912E】Prime Gift
Codeforces 912 E 题意:给\(n\leq16\)个素数\(p_1..p_n\),求第\(k\)个所有质因数都在\(n\)个数中的数. 思路:折半搜索...我原来胡搞毛搞怎么也搞不动\( ...
- JSON WEB TOKEN,简单谈谈TOKEN的使用及在C#中的实现
十年河东,十年河西,莫欺少年穷. 学无止境,精益求精. 突然发现整个十月份自己还没有写一篇博客......哎,说出来都是泪啊,最近加班实在实在实在是太多了,真的没有多余的时间写博客.这不,今天也在加班 ...