location is one of the most useful of the BOM objects.
window.location and document.location references to the same object.
8.2 all of the properties the object location :( omitted location prefix for each attribute)
hash "#contents" return to the url hash (# number followed by zero or more characters)
Host "www.wrox.com:8080" returns the server name and port number (if any)
hostname server name "www.wrox.com" with no port number
href "http://www.wrox.com" 完整url
pathname "/ WileyCDA /" directory and file name in the URL
Prot "8080" port number
Protocol "HTTP:" page using the protocol. http: or https:
Search "? q = JavaScript" in the URL of the query string
8.2.1 query string parameters
Create a function that each query string parameters.
function getQueryStingArgs() { // 查询字符串并去掉开头的?号 var qs = (location.search.length>0?location.search.substring(1):''), // 保存数据的对象 args = {}, //取得每一项 items = qs.length?qs.split("&"):[], item = null, name = null, value = null, // 在for循环中使用 i = 0, len = items.length; // 逐个将每一项添加到args对象中 for(i=0;i<len;i++){ item = items[i].split("="); name = decodeURIComponent(item[0]); value = decodeURIComponent(item[1]); if(name.length){ args[name] = value; } } return args; }
8.2.2 operating position
Change the location of the browser
window.location = "http://www.wrox.com";
location.href = "http>//www.wrox.com"
Modify the location object properties can also be changed to load the page
// 初始URL "http://www.wrox.com/WileyCDA/ "
// The URL changed to "http://www.wrox.com/WileyCDA/#section1"
location.hash = "#section1"
// The URL changed to "http://www.wrox.com/WileyCDA/?q=javascript"
location.search = "?q=javascript";
// The URL changed to "http://www.yahoo.com/WileyCDA/"
location.hostname = "www.yahoo.com";
// The URL changed to "http://www.yahoo.com/mydir/"
location.pathname = "mydir";
// The URL changed to "http://www.yahoo.com:8080/WileyCDA/"
location.port = 8080;
Prevent users back to the previous page:
location.replace("http://www.wrox.com/")
Reload:
location.reload () // possible heavy cache load
location.reload (true) // reloaded from the server