2012-01-16 199 views
1

當我將我的'網頁應用程序'添加到我的iPhone上的主屏幕上,然後打開它並單擊鏈接時,它會打開Safari。iPhone WebApp在Safari中打開鏈接

我發現一對夫婦到我的問題的解決方案,但他們似乎並不工作:

iPhone Safari Web App opens links in new window

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

if (href.indexOf(location.hostname) > -1) 
{ 
    event.preventDefault(); 
    window.location = href; 
} 
}); 

http://jakeboyles.com/2011/01/16/how-to-build-an-iphone-and-ipad-web-app/

<a ontouchstart="window.location=yourlink.html' ">Your Link</a> 

這些職位/教程的兩個是在iOS5之前編寫的。有沒有新的方法?難道我做錯了什麼?

感謝您的幫助

+0

你的問題是什麼?你只說了會發生什麼。 – Geuis 2012-01-17 04:05:27

+0

[iPhone Safari Web App在新窗口中打開鏈接]的可能重複項(http://stackoverflow.com/questions/2898740/iphone-safari-web-app-opens-links-in-new-window) – 2012-04-07 18:54:00

回答

1

一個想法就是讓你的主屏幕應用程序的iframe,並使得所有錨瞄準它。沒有JavaScript需要。

或者:

$('a').on('click touchend', function(ev){ 

    $(this).preventDefault(); 
    window.location = $(this).attr('href'); 

}); 

不要使用 「touchstart」 這樣的事情聯繫。您只需要檢測用戶何時停止觸摸鏈接。用於檢測按鍵,鼠標點擊等的同樣的事情。其想要檢測的事件的結束

.live已在最新版本的jQuery中被棄用,所以從現在開始使用.on(),它可以無縫地包裹現存和非現存的元素。

0

我寫了你所指的文章。我仍然使用我的JavaScript方法,它適用於我。 touchstart適用於網絡應用程序,因爲瀏覽器渲染它需要一些時間。如果您計劃使用原生應用程序,那麼請不要使用它,但我已經使用touchstart來處理許多應用程序,並且工作正常。

感謝 傑克博伊爾

1

這段JavaScript代碼適用於iOS 5的(它的工作對我來說):

在head標籤:

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

在鏈接,你想成爲在同一窗口打開:

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

該代碼基於th是的評論:http://mobile.tutsplus.com/tutorials/iphone/iphone-web-app-meta-tags/comment-page-1/#comment-10699

1

這對iPhone 5

$(document).ready(function(){ 

    // Stop the default behavior of the browser, which 
    // is to change the URL of the page. 
    $("a").click(function(event){ 
     event.preventDefault(); 
     // Manually change the location of the page to stay in 
     // "Standalone" mode and change the URL at the same time. 
     window.location = $(this).attr('href'); 
    }); 
}); 

記住鏈接到jQuery的最新版本的罰款。

相關問題