---
title: "self.location.href的具体用法"
author: "fastwind"
date: "2008-04-23T13:08:26Z"
tags: []
slug: "self-location-href"
draft: false
lastmod: "2008-04-23T13:08:26Z"
id: 185860
url: "https://fastwind.iteye.com/blog/185860"
---

self指代当前窗口对象,属于window最上层的对象;  
location.href 指的是某window对象的URL地址.  
self.location.href指当前窗口的URL地址,去掉self默认为当前窗口的URL地址.  
一般用于防止外部引用,如果你的网页地址是:  
[http://www.a.com](http://www.a.com/)  
别人的是[http://www.b.com](http://www.b.com/), 他在他的页面用iframe等框架引用你的[http://www.a.com](http://www.a.com/),那么你可以用:  
if(top.location.href!=self.location.href){  
location.href="[http://www.a.com](http://www.a.com/)";  
}  
来转向你的页面,  
top指代的是主体窗口,这里top.location.href返回[http://www.b.com](http://www.b.com/);  
[http://www.b.com](http://www.b.com/)!=[http://www.a.com](http://www.a.com/),返回为真(true),则网页重定向到你的网页,做到防盗用的作用.