js 用 window.open()方法跳转到新页面并且用pos

2020-06-23  本文已影响0人  愤怒的阿昆达

1.一般地,如果想打开新页面跳转到某网页,这么做:window.open(url, '_blank');
2.如果要求传参,则:window.open(url+'?id=1', '_blank');
3.如果要求post传参,则需要构建form进行submit了:

openPostWindow('http://localhost:8082/Report/a.jsp',{
    id : 1,
    userName : 'zs',
    password : '123',
    loginUrl : 'aaa',
    reportUrl : 'bbb',
});

function openPostWindow(url,params){
    var tempForm = document.createElement("form");
    tempForm.id = "tempForm1";
    tempForm.method = "post";
    tempForm.action = url;
    tempForm.target="_blank"; //打开新页面

    for(var key in params){
        tempForm.appendChild(generateInput(key,params));
    }
    if(document.all){
        tempForm.attachEvent("onsubmit",function(){});//IE
    }else{
        var subObj = tempForm.addEventListener("submit",function(){},false);//firefox
    }
    document.body.appendChild(tempForm);
    if(document.all){
        tempForm.fireEvent("onsubmit");
    }else{
        tempForm.dispatchEvent(new Event("submit"));
    }
    tempForm.submit();
    document.body.removeChild(tempForm);
}

function generateInput(key,params){
    var hideInput = document.createElement("input");
    hideInput.type = "hidden";
    hideInput.name = key;
    hideInput.value = params[key];
    return hideInput;
}
接收参数的话,比如jsp:
<%@ page import="com.aa.util.StrUtils" %>
  <%
  //获取参数&字符过滤
  int id = StrUtils.toInt(request.getParameter("id"));
  String userName = StrUtils.toEscapeString(request.getParameter("userName"));
  String password = StrUtils.toEscapeString(request.getParameter("password"));
  String loginUrl = request.getParameter("loginUrl");
  String reportUrl = request.getParameter("reportUrl");
%>
上一篇 下一篇

猜你喜欢

热点阅读