Get current URL with jQuery?

后端 未结 30 2938
你的背包
你的背包 2020-11-22 02:44

I am using jQuery. How do I get the path of the current URL and assign it to a variable?

Example URL:

http://localhost/menuname.de?foo=bar&nu         


        
30条回答
  •  面向向阳花
    2020-11-22 03:29

    window.location is an object in javascript. it returns following data

    window.location.host          #returns host
    window.location.hostname      #returns hostname
    window.location.path          #return path
    window.location.href          #returns full current url
    window.location.port          #returns the port
    window.location.protocol      #returns the protocol
    

    in jquery you can use

    $(location).attr('host');        #returns host
    $(location).attr('hostname');    #returns hostname
    $(location).attr('path');        #returns path
    $(location).attr('href');        #returns href
    $(location).attr('port');        #returns port
    $(location).attr('protocol');    #returns protocol
    

提交回复
热议问题