仰天一笑

昨日不悔,今日勿失,明日莫忧! —徐羽

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  230 随笔 :: 27 文章 :: 791 评论 :: 39 Trackbacks

先前写了一片用window.location.href实现刷新另个框架页面 ,特此我看了一下locaiton的详细用法,对此有点改进,现在我将他整理成js,方便查阅,也贴上和朋友们分享一下,具体如下:

第一、简单介绍一下location属性、用法以及相关示例:
Location
包含了关于当前 URL 的信息。

描述
location 对象描述了与一个给定的 Window 对象关联的完整 URL。location 对象的每个属性都描述了 URL 的不同特性。
通常情况下,一个 URL 会有下面的格式:

协议//主机:端口/路径名称#哈希标识?搜索条件 例如:

http://skylaugh.cnblogs.com/index.html#topic1?x=7&y=2 这些部分是满足下列需求的:

“协议”是 URL 的起始部分,直到包含到第一个冒号。
“主机”描述了主机和域名,或者一个网络主机的 IP 地址。
“端口”描述了服务器用于通讯的通讯端口。
路径名称描述了 URL 的路径方面的信息。
“哈希标识”描述了 URL 中的锚名称,包括哈希掩码(#)。此属性只应用于 HTTP 的 URL。
“搜索条件”描述了该 URL 中的任何查询信息,包括问号。此属性只应用于 HTTP 的 URL。“搜索条件”字符串包含变量和值的配对;每对之间由一个“&”连接。

属性概览
hash: Specifies an anchor name in the URL.
host: Specifies the host and domain name, or IP address, of a network host. 
hostname: Specifies the host:port portion of the URL. 
href: Specifies the entire URL. 
pathname: Specifies the URL-path portion of the URL. 
port: Specifies the communications port that the server uses. 
protocol: Specifies the beginning of the URL, including the colon. 
search: Specifies a query.

方法概览
reload Forces a reload of the window's current document. 
replace Loads the specified URL over the current history entry. 


主要功能示例,其他类同:
hash:

newWindow.location.href = http://skylaugh.cnblogs.com
newWindow.location.hash = #59831


host
A string specifying the server name, subdomain, and domain name.
newWindow.location.href =   http://skylaugh.cnblogs.com
newWindow.location.host = skylaugh.cnblogs.com


href
A string specifying the entire URL.

window.location.href="http://home.netscape.com/"


pathname
A string specifying the URL-path portion of the URL.

search
A string beginning with a question mark that specifies any query information in the URL.

newWindow.location.href = http://skylaugh.cnblogs.com
newWindow.location.search = ?newsid=111

二、location之页面跳转js如下:
//简单跳转
function gotoPage(url)
{
// eg. var url = "newsview.html?catalogid="+catalogID+"&pageid="+pageid;
window.location = url;
}
// 对location用法的升级,为单个页面传递参数
function goto_catalog(iCat)
{
if(iCat<=0)
{
top.location = "../index.aspx"; // top出去
}
else
{
window.location = "../newsCat.aspx?catid="+iCat;
}
}
// 对指定框架进行跳转页面,二种方法皆可用
function goto_iframe(url)
{
parent.mainFrame.location = "../index.aspx"; //
// parent.document.getElementById("mainFrame").src = "../index.aspx";// use dom to change page // 同时我增加了dom的写法
}
// 对指定框架进行跳转页面,因为 parent.iframename.location="../index.aspx"; 方法不能实行,主要是 "parent.iframename" 中的iframename在js中被默认为节点,而不能把传递过来的参数转换过来,所以用dom实现了该传递二个参数的框架跳转页面,希望那位仁兄不吝赐教!
function goto_iframe(iframename,url)
{
parent.document.getElementById(iframename).src = "../index.aspx";// use dom to change page by iframeName

//}
// 回到首页
function gohome()
{
top.location = "/index.aspx";
}
</script>

posted on 2006-12-20 15:56 仰天一笑 阅读(4249) 评论(4)  编辑 收藏 所属分类: 原创天地Javascript/Ajax/XML

评论

#1楼  2006-12-20 16:19 韦恩卑鄙      
所以说你搞定的是dom 而不是location
  回复  引用  查看    

#2楼 [楼主] 2006-12-20 16:57 仰天一笑      
@韦恩卑鄙
因为 parent.iframename.location="../index.aspx"; 方法不能实行,主要是 "parent.iframename" 中的iframename在js中被默认为节点,它不能把传递过来的参数转换过来,所以用Dom实现了,如果有什么高见请指教


  回复  引用  查看    

#3楼  2006-12-20 17:10 韦恩卑鄙      
呵呵 你原来的parent.iframename.location也是dom阿

凡是js在explorer访问的页面对象 都是dom方式呢

你所说的"dom方式" 其实是通过dom中的查找功能定位一个元素引用的集合查找

你的意思是直接访问parent.iframename的实例不行 就用查找的方式返回一个引用

这就是说你搞定的是dom的另一种访问方式 而不是location这个成员的用法。


至于你parent.iframename 不能直接访问 很可能是因为你在parent 页面设置了若干叫做iframename的控件 或者有js变量名也叫iframename 造成的。 参数、控件、变量、属性 命名要有所区别,不然很容易出错哦
  回复  引用  查看    

#4楼 [楼主] 2006-12-20 19:02 仰天一笑      
@韦恩卑鄙
不好意思,
1、我的文章标题产生了歧义,我说的是跳转页面的location用法,即不用open()或Redirect
2、文章我已经更新过了,加了介绍location的知识点
3、有什么问题请指教
  回复  引用  查看    


标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2006-12-20 19:14 编辑过
 
另存  打印