How to get Page url with java script

JavaScript Window Location

The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page.
Some examples:

window.location.href returns the href (URL) of the current page
window.location.hostname returns the domain name of the web host
window.location.pathname returns the path and filename of the current page
window.location.protocol returns the web protocol used (http: or https:)
window.location.assign loads a new document

1
2
3
4
5
6
7
8
if (window.location.pathname == '/xyz.php') {
$("#button1").click(function(){'
//do something
})
}else if(window.location.pathname == '/xyz1.php'){
$("#mytextfield").hover(){
alert('message');
}