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', {sysid:sysid}, 'POST', 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,"_blank");
}, function(e) {
    layer.alert('出問題啦~請稍后再試~',{title:'提示',icon: 2});
}, false); //同步

    /**
     * 動態創建form表單 - 實現post帶參數跳轉到新tab頁
     **/
    function openPostWindow(url,params,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可以在一個網站上打開另外的一個網站的地址 。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM