Tell me how to get the file name.

there is such a link

http://utp/ViewerJS#../files/EducationalMaterials/K2/P2/соц%20пакет_обновл_за%202017-18.odp 

need to get a beautiful file name

 соц_пакет_обновл_за_2017-18.odp 

do so This is a function from ViewerJs (library for viewing ODF documents)

 window.onload = function() { var a = document.location.hash.substring(1), e = s(document.location), c; a ? (e.title || (e.title = a.replace(/^.*[\\\/]/, '')), e.documentUrl = a, b(a, function(b) { b || (b = e.type ? f(e.type) : x(a)); b ? "undefined" !== String(typeof loadPlugin) ? loadPlugin(b.path, function() { c = b.getClass(); new Viewer(new c, e) }) : (c = b.getClass(), new Viewer(new c, e)) : new Viewer })) : new Viewer };` 

I get the following

 %D1%81%D0%BE%D1%86%20%D0%BF%D0%B0%D0%BA%D0%B5%D1%82_%D0%BE%D0%B1%D0%BD%D0%BE%D0%B2%D0%BB_%D0%B7%D0%B0%202017-18.odp 
  • correct the title) - Sonic Myst

4 answers 4

One way to do it like this

 function getFileNameFromUrl(url) { return decodeURI(url.split('/').pop()); } const u = 'http://utp/ViewerJS#../files/EducationalMaterials/K2/P2/соц%20пакет_обновл_за%202017-18.odp'; console.log(getFileNameFromUrl(u)); 

     let str = "http://utp/ViewerJS#../files/EducationalMaterials/K2/P2/соц%20пакет_обновл_за%202017-18.odp" let res = decodeURI(str.substring(str.lastIndexOf('/')+1,str.length)); console.log(res); 

      Slightly finalized the first answer.

       function getFileNameFromUrl(url) { return decodeURI(url.split('/').pop()); } const u = 'http://utp/ViewerJS#../files/EducationalMaterials/K2/P2/соц%20пакет_обновл_за%202017-18.odp'; console.log(getFileNameFromUrl(u).replace(/\s/g, "_")); 

      • It does not work (Above added function from library. - BorsheC

      I solved the problem like this:

       var a = decodeURI(document.location.hash.substring(1))