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

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

作者: 愤怒的阿昆达 | 来源:发表于2020-06-23 14: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");
%>

相关文章

网友评论

      本文标题:js 用 window.open()方法跳转到新页面并且用pos

      本文链接:https://www.haomeiwen.com/subject/jschfktx.html