1. 程式人生 > 程式設計 >簡單瞭解JS開啟url的方法

簡單瞭解JS開啟url的方法

這篇文章主要介紹了簡單瞭解JS開啟url的方法,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

在新標籤頁中get方式開啟url

window.open(loginurl_withaccout,"_blank");

下面根據後臺返回的url以及使用者名稱密碼欄位,以及使用者名稱密碼動態生成了帶賬號的url。

$.ax('./front/getURLBySidAndUid',{sysid:sysid},'POST',function(d) {
  var loginurl_withaccout = d.loginurl + "?"+d.namefield+"="+d.username+"&"+d.pwdfield+"="+d.userpwd;
  console.info(loginurl_withaccout);
  window.open(loginurl_withaccout,"_blank");
},function(e) {
  layer.alert('出問題啦~請稍後再試~',{title:'提示',icon: 2});
},false); //同步

在新標籤頁中post方式開啟url

下面這種方式支援IE9以上以及谷歌火狐.但是不支援360

/*獲取系統帶引數的登入url*/
$.ax('./front/getURLBySidAndUid',function(d) {

  /*get跳轉*/
  /*var loginurl_withaccout = d.loginurl + "?"+d.namefield+"="+d.username+"&"+d.pwdfield+"="+d.userpwd;
  window.open(loginurl_withaccout,"_blank");*/

  /*post跳轉*/
  var params = new Array();
  params.push({ name:d.namefield,value:d.username},{name:d.pwdfield,value:d.userpwd});
  openPostWindow(d.loginurl,params,false); //同步

  /**
   * 動態建立form表單 - 實現post帶引數跳轉到新tab頁
   **/
  function openPostWindow(url,name){
    var tempForm = document.createElement("form");
    tempForm.id="tempForm_post";
    tempForm.method="post";
    tempForm.enctype="application/x-www-form-urlencoded";
    tempForm.action=url;
    tempForm.target=name; /*開啟新視窗*/
    tempForm.style.display = "none";
    //新增引數
    for (var item in params) {
      var input = document.createElement("input");
      input.name = params[item].name;
      input.value = params[item].value;
      tempForm.appendChild(input);
    }
    document.body.appendChild(tempForm);
    tempForm.submit();
    document.body.removeChild(tempForm);
  }

window.location和window.open區別

性質不同

  • window.location:window.location是window物件的屬性。
  • window.open:window.open是window物件的方法。

用途不同

  • window.location:window.location用來替換當前頁,也就是重新定位當前頁 。
  • window.open:window.open用來讓連結頁面在視窗中開啟。

開啟網站不同

  • window.location:window.location只能在一個網站中開啟本網站的網頁。
  • window.open:window.open可以在一個網站上開啟另外的一個網站的地址 。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。