ajax回调打开新窗体防止浏览器拦截,就这么做!
问题剖析:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function
click_fun(){
window.open(
"www.baidu.com"
);
//能打开
$.ajax({
'url'
:
'${pageContext.request.contextPath}/activity/savePrizes.htm'
,
'type'
:
'post'
,
'dataType'
:
'json'
,
'data'
: data,
success:
function
(data) {
window.open(
"www.baidu.com"
);
//被拦截
},
error:
function
(){
}
});
}
|
分析:
打开新窗体只能在点击事件内触发,点击事件内的回调函数内打开窗体会被拦截,浏览器会认为是广告弹窗之类的代码
解决1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function
click_fun_new(){
var
tempwindow=window.open();
//先打开临时窗体,由于是点击事件内触发,不会被拦截
$.ajax({
'url'
:
'${pageContext.request.contextPath}/activity/savePrizes.htm'
,
'type'
:
'post'
,
'dataType'
:
'json'
,
'data'
: data,
success:
function
(data) {
tempwindow.location =
"www.baidu.com"
;
//当回调的时候更改临时窗体的路径
},
error:
function
(){
tempwindow.close();
//回调发现无需打开窗体时可以关闭之前的临时窗体
}
});
}
|
解决2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function
click_fun_new(){
var
flag =
false
;
$.ajax({
'url'
:
'${pageContext.request.contextPath}/activity/savePrizes.htm'
,
'type'
:
'post'
,
'dataType'
:
'json'
,
'data'
: data,
'async'
:
false
,
//同步请求
success:
function
(data) {
$(
"#a"
).attr(
"href"
,
"www.baidu.com"
);
//当回调的时候更改页面上或创建的某个a标签的href
flag =
true
;
//更改标志
},
error:
function
(){
}
});
if
(flag){
$(
"#a"
).click();
//href属性更改后模拟点击
}
}
|
以上就是ajax回调打开新窗体防止浏览器拦截的两种方法,希望对大家的学习有所帮助。
http://www.jb51.net/article/83743.htm