最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Safari show Pop-Up on window.open - Stack Overflow

programmeradmin4浏览0评论

hope someone can help me with this. The code snippet works fine. The only thing is on an iPad/Safari this code opening new tab runs into the pop-up blocker. If we disable the pop-up blocker (which would be OK) there is still a message showing up "This site is attempting to open a pop-up window" with the buttons "Block" and "Allow" - screenshot attached. Clicking "Allow" opens the new tab.

PopUp

How to avoid the pop-up blocker and this message?

Try 1 (popup shows up):

var popUp = window.open(SurveyLinkval);
try {
	popUp.focus();   
}catch (e) {
	alert("Pop-up Blocker is enabled! Please disable your pop-up blocker.");
}

hope someone can help me with this. The code snippet works fine. The only thing is on an iPad/Safari this code opening new tab runs into the pop-up blocker. If we disable the pop-up blocker (which would be OK) there is still a message showing up "This site is attempting to open a pop-up window" with the buttons "Block" and "Allow" - screenshot attached. Clicking "Allow" opens the new tab.

PopUp

How to avoid the pop-up blocker and this message?

Try 1 (popup shows up):

var popUp = window.open(SurveyLinkval);
try {
	popUp.focus();   
}catch (e) {
	alert("Pop-up Blocker is enabled! Please disable your pop-up blocker.");
}

Try 2 (popup shows up):

$('#newTabLink').attr('href', SurveyLinkval);
$('#newTabLink')[0].click();

Below HTML code does work:

<a id="newTabLink" target="_blank" href="https://www.google./" style="display:block;">Open in New Tab Link</a>
Share Improve this question asked Dec 10, 2019 at 18:30 Mario ZMario Z 211 gold badge1 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

This is not possible to "fix" in the browsers. Some browsers, like Safari don't allow open popups if the user doesn't do any action.

Each action or Event can be "Trusted" or not. The event will be trusted if the user makes any interaction to fire the event in another case the context will be Untrusted.

In both cases, you use javascript without user interaction. So, to prevent unexpected windows, Safari will block the popups.

If you put your code in a function and you fire it with onClick, when the user makes the click (user interaction) the context of the event will be trusted and the window.open will be performed without any inconvenient.

For example:

<html>
    <body>
        <span onclick="onAnyTextClick()">anyText</span>
        <script>
            function onAnyTextClick() {
                var popUp = window.open("https://www.google.");
                try {
                    popUp.focus();
                }catch (e) {
                    alert("Pop-up Blocker is enabled! Please disable your pop-up blocker.");
                }
            }
        </script>
    </body>
</html>

Good to know: https://www.w3/TR/uievents/#trusted-events

Maybe it could help, To open an URL with Safari (version 16.0), which did not consider it was a popup, with the following code:

// Set my target URL
var newUrl = "http://www.myurl.";
// Create an <a> tag with JQuery
var newa = $('<a id="newiddocdl" href="' + newUrl + '" target="_blank"></a>');
// Append this tag in the body part (it will remain invisible)
$('body').append( newa );
// Wait a little for the DOM to record this new tag. 50ms is arbitrary
setTimeout( function()
{
    // Simulate a user click
    $('#newiddocdl')[0].click();
    // The new window, or tab, will open, free the new <a> tag from body
    $('#newiddocdl').remove();
}, 50 );
发布评论

评论列表(0)

  1. 暂无评论