2017-04-19 125 views
1

所以我有一個圖像,蘋果音樂徽章。一旦用戶點擊徽章,我想要一個彈出窗口詢問用戶是否想要顯示歌曲的清晰版本或清潔版本,並且相應的按鈕可以執行該操作。Javascript彈出重定向

目前我有一個JavaScript彈出窗口,它應該允許用戶按「確定」重定向到顯式版本,然後按「取消」重定向到清潔版本。這個選項對我來說工作正常,但是按鈕並沒有做我期望他們做的事,兩個按鈕都重定向到了顯式版本。

這是我的HTML和JavaScript至今..

<a class="AppleMusic" 
    href="**explicit-link**" 
    style="display:inline-block; 
      overflow:hidden; 
    background:url(https://linkmaker.itunes.apple.com/assets/shared/badges/en-us/music-lrg.svg) no-repeat; 
      width:150px; 
      height:55px; 
      background-size:contain;"> 
</a> 

<script type='text/javascript'> 
    $(window).on('load', function() { 
     $(".AppleMusic").on("click", function (event) { 
      if (confirm("This will redirect to the explict version of the song. Press 'Cancel' If you'd like to be redirected to the Clean version.")) { 
       return true; 
      } 
      else { 
       window.location = "**clean-link**"; 
      } 
     }); 
    }); 
</script> 

的問題是,無論他們點擊什麼,它重定向到顯性(geo.itunes.apple.com)網址。

我想不使用UI對話框,如果可能的話。

+0

didntn讓你的要求嗎?請更具體 –

回答

1

試着改變你的函數是:

$(".AppleMusic").on("click", function(event){ 
     event.preventDefault(); 
     if (confirm("This will redirect to the explict version of the song. Press 'Cancel' If you'd like to be redirected to the Clean version.")){ 
     window.location = "https://geo.itunes.apple.com/us/album/feelinme-feat-adrian-stresow/id1224174169?i=1224174173&mt=1&app=music&at=1l3vwYm&ct=FEELINME" 
     } 
     else { 
     window.location = "https://www.google.com/search?site=&q=clean+url"; 
     } 
}); 

防止默認會,因爲它說,防止點擊的對象的默認行爲。

+0

謝謝,這是我所需要的東西。 – ddshd

0
$(function() { 
    $(".AppleMusic").on("click", function(event) { 
     event.preventDefault(); 
     if (confirm("This will redirect to the explict version of the song. Press 'Cancel' If you'd like to be redirected to the Clean version.")){ 
      var location = "https://geo.itunes.apple.com/us/album/feelinme-feat-adrian-stresow/id1224174169?i=1224174173&mt=1&app=music&at=1l3vwYm&ct=FEELINME"; 
      window.open(location, "_self"); 
     } else { 
      var location = "https://www.google.com/search?site=&q=clean+url"; 
      window.open(location, "_self"); 
     } 
    }); 
}); 

作品一脈相承的URL