JS – Location Href – Pathname – Assign

Get Location Href and Get Location Pathname:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html>
<body>
 
<script>
// Location Href
document.write("Location Href: " + location.href + "<br>");
// Location Pathname
document.write("Location Pathname: " + location.pathname);
 
</script>
 
</body>
</html>

Sample result on web:
Location Href: http://www.lucedigitale.com/content/sample.html
Location Pathname: /content/sample.html

Sample result on local:
Location Href: file:///C:/Users/Utente/Desktop/content/sample.html
Location Pathname: /C:/Users/Utente/Desktop/content/sample.html

Load a new document inside current window:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
<script>
function newDoc()
  {
  window.location.assign("http://www.lucedigitale.com")
  }
</script>
</head>
<body>
 
<input type="button" value="Load new document" onclick="newDoc()">
 
</body>
</html>

My official WebSite >