zoukankan      html  css  js  c++  java
  • 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

    1. var url = window.location.pathname;

    This declares the url variable and adds the current pathname as its value.

    1. var filename = url.substring(url.lastIndexOf('/')+1);
    2. 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

    1. var url = window.location.pathname;
    2. var filename = url.substring(url.lastIndexOf('/')+1);
    3. 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

  • 相关阅读:
    python之路3-元组、列表、字典、集合
    python之路2-字符串操作
    Python之路1-变量、数据类型、循环语法
    config模块
    os模块
    logging模块
    控制台报错定位问题所在
    time模块
    random模块
    列表生成
  • 原文地址:https://www.cnblogs.com/rgqancy/p/7269503.html
Copyright © 2011-2022 走看看