Javascript Get Website URL

后端 未结 7 2026
别那么骄傲
别那么骄傲 2020-12-29 05:37

How do I get Javascript to tell me the website url.

For example if I have a page www.example.com/page.html

I want Javascript to tell me the site

7条回答
  •  抹茶落季
    2020-12-29 06:19

    There are several ways you can do this, but one way might be best for certain situations (e.g. within an iFrame).

    Protocol + Domain + Page

    document.URL
    > "http://example.com/page1.html"
    
    document.location.href
    > "http://example.com/page1.html"
    

    Protocol + Domain

    document.location.origin
    > "http://example.com"
    

    Domain

    document.location.host
    > "example.com"
    

    Page

    document.location.pathname
    > "/page1.html"
    

提交回复
热议问题