window.open a.href打开窗口referer的问题:

JSP:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>045index</title>
<style type="text/css">
a {
border: 1px solid blue;
    margin-left: 5px;
}
</style>
</head>
<body>
<input type="button" value="window.open:_blank" id="btn001">
<input type="button" value="window.open:winopen" id="btn002">
<input type="button" value="window.open:winopen anotherButton" id="btn003">
<input type="button" value="see how many window open" id="btn004">
<input type="button" value="close windows opened" id="btn005">
<!-- a href 打开的窗口对象不能直接获得到 -->
<a href="test8.do" target="_blank">a link _blank </a>
<!-- 如果target对象一致,那么打开的窗口就是一致的 -->
<a href="test8.do" target="winopen">a link winopen </a>
<a href="test8.do" target="winopen">a link winopen2 </a>
<!-- 使用a的click事件进行新窗口的打开,调用button的事件 -->
<a id="a001" >a link click _blank </a>
<a href="test8.do" target="winopen" id="a002" >a link click winopen and href </a>
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="b/js/bootstrap.js"></script>
<script type="text/javascript" src="js/index046.js"></script>
</body>
</html>

js:

/**1, 用window.open的时候可以得到打开的窗口对象,可以用来后期关闭;
* 2, 用a.href可以打开新的窗口,但是得不到打开的窗口对象,后期无法关闭;
* 3, window.open/a.href的target设置为一个固定值winopen,反复打开的窗口都会是同一个窗口;
* 4, chrome中window.open/a.href都会有request.getHeader("referer");
* 5, IE中的window.open没有referer,a.href会有referer;
* 6, 用<a href="test8.do" target="winopen" id="a002" >的方式可以hack一下,
* window.open的方式如果没有referer,会显示空页面,而a.href会打开同样的窗口,这个有
* referer,会显示正常的内容;
*/
$(function() {
$('#btn001').click(btn001Click);
$('#btn002').click(btn002Click);
$('#btn003').click(btn003Click);
$('#btn004').click(btn004Click);
$('#btn005').click(btn005Click);
$('#a001').click(btn001Click);
$('#a002').click(btn002Click);
});
var opens = [];
function btn001Click(e) {
// 反复点击会打开新的窗口;
// 用chrome进行打开,会有request.getHeader("referer")
// 用IE进行打开,request.getHeader("referer")会是null;
var open = window.open('test8.do', '_blank');
// chrome中第一次打开窗体有时无法正确设置title
// IE中可以正常的设置titile
open.document.title='a';
opens.push(open);
// return false是为了抑制a的默认事件;
return false;
}
function btn002Click(e) {
// 反复点击会打开同一个窗口
var open = window.open('test8.do', 'winopen');
// chrome中第一次打开窗体有时无法正确设置title,第二次打开同一个窗体的时候可能会设置正确
// IE中可以正常的设置titile
open.document.title='b';
// 会在opens数组中重复添加相同的窗口对象;
opens.push(open);
}
function btn003Click(e) {
// 反复点击会打开同一个窗口,与btn002Click的窗口一致
var open = window.open('test8.do', 'winopen');
// chrome中第一次打开窗体有时无法正确设置title,第二次打开同一个窗体的时候可能会设置正确
// IE中可以正常的设置titile
open.document.title='c';
// 会在opens数组中重复添加相同的窗口对象;
opens.push(open);
}
function btn004Click(e) {
console.log(opens);
}
function btn005Click(e) {
var i = 0, len = opens.length;
for (i = 0; i < len; i++) {
console.log(i);
// 如果数组中有重复对象,在前面已经关闭了,后面重复关闭不会报异常;
opens[i].close();
}
}

servlet:

package com.stono.servlet;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
public class TestServlet8 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
String referer = req.getHeader("referer");
System.out.println(referer);
if(referer == null){
return ;
}
PrintWriter writer = resp.getWriter();
writer.println("this is from com.stono.servlet.TestServlet8");
};
}

关注一下:a.href的一个坑:http://www.cnblogs.com/iyangyuan/p/3979957.html

04-24 07:29