How do get URL in javascript?

This question already has an answer here:

  • Get current URL in JavaScript? 29 answers

  • To get the entire current URL, use:

    var currentURL = window.location;
    

    Then to get the hash, without the # itself, use:

    var trimmedHash = window.location.hash.substr(1);
    // returns `21` from `xxxx.com/#21`
    

    window.location.hash returns #21, and substr(1) returns all but the first character of that string, thus removing the # .


    Use next:

    HREF = document.location.href
    HASH = document.location.hash
    

    http://www.w3schools.com/jsref/prop_loc_href.asp
    http://www.w3schools.com/jsref/prop_loc_hash.asp


    try:

    window.location.hash // will result #21
    window.location.hash.substr(1) // will result 21
    

    hope that helps.

    链接地址: http://www.djcxy.com/p/70238.html

    上一篇: 如何检查当前URL中的字符串与JavaScript?

    下一篇: 如何在JavaScript中获取URL?