js a location object (BOM 2)

The location object

BOM object is a location object provides information about the current document window to load, and some of the navigation features include URL can be parsed into separate fragments. location is both a window object attributes, but also document object properties. window.location and document.location refer to the same object.

  • URL fragment

By location.hash can hash to the url

  • Query string parameters

      function getQueryStringArgs()
      {
          //1.取得查询字符串并去掉开头问号
          var qs=(location.search.length>0?location.search.substring(1):"");
          //保存数据的对象
          var args={};
          //3.取得每一项参数
          var items=qs.length?qs.split("&"):[];
          var item=null;
          var value=null;
          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]=valuel
              }
          }
          return args;
      }
  • Operating position

    • Open a new URL and a new record, jump on this page

      location.assign("https://ellenxx.com");
      //或
      window.location="https://ellenxx.com";
      //或
      location.href="http://ellenxx.com";
    • Change the current page

      The location may be a hash, search, hostname and port property to the new value to change the current page.

      //初始https://ellenxx.com/blog
      
      //将Url设置为https://ellenxx.com/blog/#s
      location.hash="#s";
      
      //将Url设置为https://ellenxx.com/blog/?page=4
      location.search="?page=4"
      ·······
      
    • Prohibit click the "back" button to return to the previous page

      replace () method: receiving a parameter to the URL to navigate to after the jump can not be returned back to the previous page

      setTimeout(function(){
                  location.replace("http://ellenxx.com");
              },1000)
    • Reload the current page

      reload () method: do not pass any parameters, the most effective way to reload, which is the last request is not changed, the page is loaded from cache; incoming true, will force a re-loaded from the server.

      location.reload();//重新加载(有可能从缓存中加载)
      location.reload(true);//重新加载(从服务器加载)

Guess you like

Origin www.cnblogs.com/ellen-mylife/p/11368567.html