javascript中的location用法简单介绍

2016-02-19 10:26 8 1 收藏

下面是个超简单的javascript中的location用法简单介绍教程,图老师小编精心挑选推荐,大家行行好,多给几个赞吧,小编吐血跪求~

【 tulaoshi.com - Web开发 】

先前写了一片用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

来源:https://www.tulaoshi.com/n/20160219/1594647.html

延伸阅读
标签: Web开发
在编写JAVASCRIPT代码过程中,有时候会经常对函数进行自调用,也就是函数递归, JAVASCRIPT允许函数递归,但是有其自己的递归原则,  view plaincopy to clipboardprint? mce:script type="text/javascript"!--   var type=6;       function current(a)       { ...
标签: Web开发
jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求 参数: url (String) : 发送请求的URL地址. data (Map) : (可选) 要发送给服务器的数据,以 Key/value 的键值对形式表示。 callback (Function) : (可选) 载入成功时回调函数(只有当Response的返回状态是success才是调用该方法)。 type (String) : ...
标签: Web开发
script function evil(){ var toint=parseInt("123"); var intvalue=123; var tostr=intvalue.toString(); var my_friends ="trixie,moxie,sven,guido,hermes";var friend_array =my_friends.split(",");for (loop=0; loop friend_array.length;loop++){document.writeln(friend_array[loop] + &q...
睡眠面膜怎么用?干燥的季节的到来,肌肤的缺水现象越发严重。很多睡眠面膜的补水功能都很好,所以在干燥的季节敷睡眠面膜来锁住肌肤的水分是最好的方法之一。今天小编为你公开睡眠面膜的用法步骤,让你四季水润不停。 第一步:清洁面部 虽说睡眠面膜是懒MM的福音,可是敷上睡眠面膜前的步骤都要做好,才能让睡眠面膜的功用最大...
标签: Web开发
使用JavaScript可以创建自己的对象。虽然JavaScript内部和浏览器本身的功能已十分强大,但JavaScript还是提供了创建一个新对象的方法。 使其不必像超文本标识语言那样,求于或其它多媒体工具,就能完成许多复杂的工作。 在JavaScript中创建一个新的对象是十分简单的。首先它必须定义一个对象,而后再为该对象创建一个实例。这个实例就是...

经验教程

783

收藏

51
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部