在我的代码中,一旦用户单击我页面上的按钮,就应该使用以下JS代码打开一个新标签页。
除了在iOS上的Google App浏览器上,它只在打开一个新的空白标签后就可以在所有浏览器上正常工作。
这里是Fiddle
如果从任何浏览器中单击“单击”,将打开一个新标签,进入“ next-page.php” 但是,如果您使用iOS并通过Google搜索应用打开此小提琴,它将仅打开一个空白标签。
似乎不支持的代码在Fiddle中的第66行:
wId = ''+new Date().getTime();
w = window.open('about:blank','myPopup_Window' + wId);
form.attr('target', 'myPopup_Window' + wId);
(我知道,因为代码中的另一个选项位于IF
中,可以起作用,尽管它具有不同的功能-它在同一页面中打开目标页面,然后打开包含原始页面的新标签。
HTML:
<label class="popup-button" data-tab-url="/next-page.php" data-tab-url-go="foo" data-popup-url="none" data-popup-url-go="foo">CLICK</label>
JS:
(function($) {
var check_mobile = true; //
var button_class1 = ".popup-button";
var button_class3 = ".popup-button-flipped";
var currentPopup = null;
var currentPopupTab = null;
var currentPopupXhr = null;
var initOpenModal = false;
var checkUrl = function(url) {
if (!url)
return url;
return url.trim();
};
var getFormData = function(form) {
var data = {};
form.find(':input').each(function() {
var n = $(this).attr('name');
if (n) {
var v = $(this).val();
data[n] = v;
}
});
return data;
};
var w = null;
var wId = 0;
var tryOpenTab2 = function(button,tab_url,tab_url_data_go) {
var data = {};
var form = button.parents('form').first();
if (form.length > 0)
data = getFormData(form);
if (tab_url_data_go) {
data['go'] = tab_url_data_go
}
if (!tab_url)
return;
var form = $('<form></form>');
form.attr('method', 'POST');
form.attr('action', tab_url);
for (var k in data) {
var input = $('<input />');
input.attr('type', 'hidden');
input.attr('name', k);
input.attr('value', data[k]);
form.append(input);
}
$('body').append(form);
if (w && !w.closed) {
w = null;
}
if (button.is(button_class3)) {
w = window.open(window.location.href.split('#')[0] + "#" + button.attr("data-popup-id"));
} else {
wId = ''+new Date().getTime();
w = window.open('about:blank','myPopup_Window' + wId);
form.attr('target', 'myPopup_Window' + wId);
//wId++;
}
window.setTimeout(function(){
form.submit();
window.setTimeout(function(){form.remove();}, 0);
},0);
};
var setPopups = function(button, no_one_time_check) {
if (typeof no_one_time_check == "undefined")
no_one_time_check = false;
var tab_url = checkUrl(button.attr("data-tab-url"));
if (tab_url == "none")
tab_url = "";
if ( initOpenModal || !button.is(button_class3)) {
var popup_url = checkUrl(button.attr("data-popup-url"));
{ //check no show
var id = button.attr("data-popup-id");
var cn = "button-" + id + "-nopopup";
}
var popup_url_data_go = checkUrl(button.attr("data-popup-url-go"));
var data = {};
if (popup_url_data_go) {
data = {'go' : popup_url_data_go};
}
}
if (!initOpenModal) {
var tab_url_data_go = checkUrl(button.attr("data-tab-url-go"));
tryOpenTab2(button, tab_url,tab_url_data_go);
}
};
$(document).ready(function(){
var id = 0;
$(button_class1 + "," + button_class3).each(function() {
$(this).attr("data-popup-id", id++);
});
$(button_class1).each(function() {
var button = $(this);
button.on('click.popup-button', function() {
setPopups(button);
return false;
});
});
});
})(jQuery);