Javascript 弹出重定向

Javascript Popup Redirect

所以我有一张图片,Apple Music 徽章。一旦用户点击徽章,我想要一个弹出窗口,询问用户是否想要播放歌曲的 Explicit 版本或 Clean 版本,相应的按钮将执行操作。

目前我有一个 javascript 弹出窗口,它应该允许用户按 "OK" 重定向到显式版本并按 "Cancel" 重定向到干净版本。这个选项对我来说很好,但是按钮没有按照我的预期执行,两个按钮都重定向到显式版本。

这是我目前的 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) url。

如果可能,我不想使用 UI 对话框。

尝试将您的函数更改为:

  $(".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";
      }
});

如其所说,阻止默认将阻止被单击对象的默认行为。

$(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");
         }
     });
});

使用同源网址