Get page url using javascript

前端 未结 4 434
滥情空心
滥情空心 2020-12-18 00:27

Could someone recommend a way to get page name from a url using JavaScript?

For instance if I have:

http://www.cnn.com/news/1234/news.html?a=1&b         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 00:57

    You might want to also find file paths on a local disk, and you might not want to include any hash or get strings in the path-

    String.prototype.fileName= function(){
     var f, s= this.split(/[#\?]/, 1)[0].replace(/\\/g,'/');
     s= s.substring(s.lastIndexOf('/')+ 1);
     f=  /^(([^\.]+)(\.\w+)?)/.exec(s) || [];
     return f[1] ||  '';
    }
    

提交回复
热议问题