Get page url using javascript

前端 未结 4 433
滥情空心
滥情空心 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 01:08

    You can do this pretty easily via window.location.pathname parsing:

    var file, n;
    
    file = window.location.pathname;
    n = file.lastIndexOf('/');
    if (n >= 0) {
        file = file.substring(n + 1);
    }
    alert(file);
    

    ...or as others have said, you can do it with a regexp in one line. One kinda dense-looking line, but with a comment above it, should be a good way to go.

提交回复
热议问题