Hướng dẫn file name 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. Show Explanation
This declares the url variable and adds the current pathname as its value.
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
Because cases tend to fail with custom code, I looked up to the JavaScript There has to be a good library out there which solves this common problem. Behold URI.js. All you need is a simple statement like the following:
Then we can create a simple function that does null checks and removes the file extension:
Here's a snippet with test cases to play around with. All cases pass except drive paths.
Results
This solution is for you if you're too lazy to write custom code and don't mind using a library to do work for you. It isn't for you if you want to code golf the solution. A URL or a web address, often has a file name, with few parameters occasionally. Sometimes, we might need to know only the file name in the URL. There's a simple technique to extract the file name from a url in JavaScript. How to replace a String with another String in the URL Bar using JavaScript or jQuery We want the file name from the url. But, first we need a url. You can use window.location.href to get the url of the current page or you can simply use the complete web address. Try it The alert will show the complete url (the web address), which will have the file name and few parameter etc. To get only the file name, I'll use the split() and pop() functions. Try it The split() function creates an array of string, after removing all the forward slash (/) from the url string. The pop() function will return the last element, which is file name, from the array. And that's what we want. How to check if the URL bar contains a Specified String in JavaScript and jQuery Things get a little tricky, when the url has parameters (also called query strings or variables) and other information. A url with query string may look like this. https://www.encodedna.com/javascript/practice-ground/default.htm?pg= accordion_menu_using_jquery_css The above (second) example will not be able to return the file name in this case. Because the split() in the second example, removes the forward slashes only. The query string now have a question mark (?). We need to get rid of this too. Here's how we can do this. Try it Its super easy to get the FileName and File Extension from a URL in Asp.Net using C# and Vb Now, it will split, pop and split again, to get the file name. Similarly, the url may have multiple parameters separated by &, it may # and lots of other information. In such cases, you might have to use the split() function multiple times. How to pass multiple variables or parameters using a QueryString in Asp.Net That's it. Thanks for reading. ☺ ← PreviousNext → How to extract file name from 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. How to get document name in JavaScript?check the doc name in javascript. var path = window. location. pathname;. var page = path. split("/"). pop();. console. log( page );. |