---
title: "JS来获取页面的URL top.location.href 和 window.location.href"
author: "duyunfengsolo"
date: "2010-03-03T11:31:03Z"
tags: []
categories: ["Web前端"]
slug: "js-url-top-location-href-window-location-href"
draft: false
lastmod: "2010-03-03T11:31:03Z"
id: 606497
url: "https://solodu.iteye.com/blog/606497"
---

JS来获取页面的URL：

(1)普通的格式是：window.location.href

(2)但是如果是登录时候先弹出个对话框，这个对话框里面是登录的页面login.jsp，这个时候在这个对话框里面如果想获取最外层的url的方式，就必须用 top.location.href 了。

 

例子如下：

```
var strHref = top.document.location.href;
var intPos = strHref.indexOf("#");
var strRight = strHref.substr(0,intPos);
if(num == "success"){
	top.location.href = strRight;
}else if(num !== "null"){
	document.getElementById("error_login").innerHTML = num;
}
```

 

另外还有一种情况是:

      在form提交表单的时候有个属性可能对上面的(2)的情况有用target="\_top"

&lt;form name="loginForm" action="Login.action" method="post" target="\_top"&gt;

&lt;/form&gt;