iPhone Safari Web App otwiera linki w nowym oknie

Mam problem z web po dodaniu ikony do ekranu głównego. Jeśli sieć zostanie uruchomiona z ekranu głównego, wszystkie łącza zostaną otwarte w nowym oknie w Safari (i utracą funkcje pełnoekranowe). Jak Mogę temu zapobiec? Nie mogłem znaleźć żadnej pomocy, tylko to samo pytanie bez odpowiedzi.

Author: ThinkingStiff, 2010-05-24

20 answers

Znalazłem rozwiązanie JavaScript w iWebKit framework:

var a=document.getElementsByTagName("a");
for(var i=0;i<a.length;i++)
{
    a[i].onclick=function()
    {
        window.location=this.getAttribute("href");
        return false
    }
}
 102
Author: Pavel Linkesch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-24 18:00:41

Inne rozwiązania tutaj albo nie uwzględniają linków zewnętrznych (które prawdopodobnie chcesz otworzyć zewnętrznie w Safari), albo nie uwzględniają linków względnych (bez domeny w nich).

Projekt HTML5 mobile-boilerplate łączy się z tym gistem, który ma dobrą dyskusję na ten temat: https://gist.github.com/1042026

Oto ostateczny kod, który wymyślili:

<script>(function(a,b,c){if(c in b&&b[c]){var d,e=a.location,f=/^(a|html)$/i;a.addEventListener("click",function(a){d=a.target;while(!f.test(d.nodeName))d=d.parentNode;"href"in d&&(d.href.indexOf("http")||~d.href.indexOf(e.host))&&(a.preventDefault(),e.href=d.href)},!1)}})(document,window.navigator,"standalone")</script>
 88
Author: rmarscher,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-17 19:34:50

Jeśli używasz jQuery, możesz zrobić:

$("a").click(function (event) {
    event.preventDefault();
    window.location = $(this).attr("href");
});
 45
Author: David H.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-01 04:45:27

To działa dla mnie na iOS 6.1 i z linkami Bootstrap JS (np. rozwijane menu itp.)

$(document).ready(function(){
    if (("standalone" in window.navigator) && window.navigator.standalone) {
      // For iOS Apps
      $('a').on('click', function(e){
        e.preventDefault();
        var new_location = $(this).attr('href');
        if (new_location != undefined && new_location.substr(0, 1) != '#' && $(this).attr('data-method') == undefined){
          window.location = new_location;
        }
      });
    }
  });
 20
Author: Sean,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-08 03:18:25

Bazując na odpowiedzi Davids i komentarzu Richardsa, powinieneś sprawdzić domenę. W przeciwnym razie linki do innych stron internetowych będą również otwierane w Twojej aplikacji internetowej.

$('a').live('click', function (event)
{      
    var href = $(this).attr("href");

    if (href.indexOf(location.hostname) > -1)
    {
        event.preventDefault();
        window.location = href;
    }
});
 5
Author: Thomas Kekeisen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-24 09:53:31

Jeśli używasz jQuery Mobile, pojawi się nowe okno podczas używania atrybutu data-ajax= 'false'. W rzeczywistości stanie się to za każdym razem, gdy ajaxEnabled jest wyłączony, będąc połączeniem zewnętrznym i przez $.mobile.ustawienie ajaxEnabled lub poprzez atrybut target=".

Możesz to naprawić używając tego:

$("a[data-ajax='false']").live("click", function(event){
  if (this.href) {
    event.preventDefault();
    location.href=this.href;
    return false;
  }
});

(podziękowania dla Richarda Poole ' a za metodę live () - nie działało z bind ())

Jeśli wyłączyłeś ajaxEnabled na całym świecie, musisz porzucić [data-ajax= 'false'].

Zajęło mi to dość długo, aby dowiedzieć się, jak spodziewałem się, że będzie to specyficzny problem jQuery Mobile, gdzie w rzeczywistości było to połączenie Ajax, które faktycznie zakazało nowego okna.

 5
Author: Jason Prawn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-07 12:23:34

To stare pytanie i wiele rozwiązań tutaj używa javascript. Od tego czasu wydano iOS 11.3 i możesz teraz używać elementu scope . Element scope to adres URL podobny do "/", gdzie wszystkie ścieżki w tym zakresie nie otwierają nowej strony.

Element scope jest ciągiem znaków, który reprezentuje zakres nawigacji kontekst aplikacji webowej.

Oto mój przykład:

{
  "name": "Test",
  "short_name": "Test",
  "lang": "en-US",
  "start_url": "/",
  "scope": "/",
  ...
}

Możesz również przeczytać więcej na ten temat tutaj . Polecam również użycie generatora , który zapewni tę funkcjonalność.

Jeśli określisz zakres, wszystko działa zgodnie z oczekiwaniami, podobnie jak Android, miejsca poza zasięgiem otworzy się w Safari - z przycisk Wstecz (mały na pasku stanu) do twojego PWA.

 5
Author: Amir Raminfar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-02 01:40:31

Ten kod działa na iOS 5 (u mnie zadziałał):

In the head tag:

<script type="text/javascript">
    function OpenLink(theLink){
        window.location.href = theLink.href;
    }
</script>

W linku, który chcesz otworzyć w tym samym oknie:

<a href="(your website here)" onclick="OpenLink(this); return false"> Link </a>

Dostałem ten kod z tego komentarza: iPhone web app meta tagi

 3
Author: SerinEleven,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-19 13:17:08

Może powinieneś zezwolić na otwieranie linków w nowym oknie, gdy target jest jawnie ustawiony na "_blank", jak również:

$('a').live('click', function (event)
{      
    var href = $(this).attr("href");

    // prevent internal links (href.indexOf...) to open in safari if target
    // is not explicitly set_blank, doesn't break href="#" links
    if (href.indexOf(location.hostname) > -1 && href != "#" && $(this).attr("target") != "_blank")
    {
        event.preventDefault();
        window.location = href;
    }

});
 3
Author: daformat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-24 11:22:21

Znalazłem taki, który jest bardzo kompletny i skuteczny, ponieważ sprawdza, czy działa tylko pod samodzielną WebApp, działa bez jQuery i jest również prosty, po prostu przetestowany pod iOS 8.2:

Pozostań samodzielny: Zapobiegaj łączom w samodzielnych aplikacjach internetowych otwierającym mobilne Safari

 3
Author: DavidTaubmann,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-14 01:13:52

Możesz też zrobić linkowanie prawie normalnie:

<a href="#" onclick="window.location='URL_TO_GO';">TEXT OF THE LINK</a>

I możesz usunąć hash tag i href, wszystko, co robi, wpływa na wygląd..

 2
Author: JuR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-29 12:05:25

To mi się udało na iOS 6 (bardzo mała adaptacja odpowiedzi rmarschera):

<script>                                                                
    (function(document,navigator,standalone) {                          
        if (standalone in navigator && navigator[standalone]) {         
            var curnode,location=document.location,stop=/^(a|html)$/i;  
            document.addEventListener("click", function(e) {            
                curnode=e.target;                                       
                while (!stop.test(curnode.nodeName)) {                  
                    curnode=curnode.parentNode;                         
                }                                                       
                if ("href" in curnode && (curnode.href.indexOf("http") || ~curnode.href.indexOf(location.host)) && curnode.target == false) {
                    e.preventDefault();                                 
                    location.href=curnode.href                          
                }                                                       
            },false);                                                   
        }                                                               
    })(document,window.navigator,"standalone")                          
</script>
 2
Author: bjh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-01 21:01:17

Jest to nieco zmodyfikowana wersja Seana, która zapobiegała wstecznemu przyciskowi

// this function makes anchor tags work properly on an iphone

$(document).ready(function(){
if (("standalone" in window.navigator) && window.navigator.standalone) {
  // For iOS Apps
  $("a").on("click", function(e){

    var new_location = $(this).attr("href");
    if (new_location != undefined && new_location.substr(0, 1) != "#" && new_location!='' && $(this).attr("data-method") == undefined){
      e.preventDefault();
      window.location = new_location;
    }
  });
}

});

 2
Author: Richard Turner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-23 15:02:29

Dla osób z Twitterem Bootstrap i Rails 3

$('a').live('click', function (event) {
  if(!($(this).attr('data-method')=='delete')){
    var href = $(this).attr("href");
    event.preventDefault();
    window.location = href; 
  }   
});

Usuń linki nadal działają w ten sposób.

 1
Author: wouf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-10 22:42:37

Wolę otwierać wszystkie linki wewnątrz trybu samodzielnej aplikacji internetowej z wyjątkiem tych, które mają target= "_blank". Oczywiście używając jQuery.

$(document).on('click', 'a', function(e) {
    if ($(this).attr('target') !== '_blank') {
        e.preventDefault();
        window.location = $(this).attr('href');
    }
});
 1
Author: Alex Haas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-04 00:39:52

Obejście, którego użyłem w aplikacji internetowej na iOS, polegało na tym, że zrobiłem wszystkie linki (które były przyciskami CSS) z przycisków wyślij. Więc otworzyłem formularz, który wysłał do linku docelowego, a następnie input type = " submit" Nie jest to najlepszy sposób, ale to właśnie odkryłem, zanim znalazłem tę stronę.

 1
Author: dster77,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-03 00:49:24

Stworzyłem pakiet bower instalowalny z @rmarscher ' s answer, który można znaleźć tutaj:

Http://github.com/stylr/iosweblinks

Możesz łatwo zainstalować fragment za pomocą bowera za pomocą bower install --save iosweblinks

 1
Author: Robin van Baalen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 10:31:13

Dla tych, którzy używają JQuery Mobile, wyskakujące okienko z powyższymi rozwiązaniami. Spowoduje to zachowanie linków w aplikacji webapp i pozwoli na wyskakujące okienka.

$(document).on('click','a', function (event) {
    if($(this).attr('href').indexOf('#') == 0) {
        return true;
    }
    event.preventDefault();
    window.location = $(this).attr('href');     
});

Można to również zrobić przez:

$(document).on('click','a', function (event){
    if($(this).attr('data-rel') == 'popup'){
        return true;
    }
    event.preventDefault();
    window.location = $(this).attr('href');     
});
 1
Author: Hexchaimen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-07 15:36:42

Oto, czego użyłbym dla wszystkich linków na stronie...

document.body.addEventListener(function(event) {
    if (event.target.href && event.target.target != "_blank") {
        event.preventDefault();
        window.location = this.href;                
    }
});

Jeśli używasz jQuery lub Zepto...

$("body").on("click", "a", function(event) {
   event.target.target != "_blank" && (window.location = event.target.href);
});
 0
Author: alex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-13 07:54:38

Możesz po prostu usunąć ten meta tag.

<meta name="apple-mobile-web-app-capable" content="yes">
 0
Author: Soohwan Park,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-29 09:41:07