简体   繁体   English

jQuery链接打开新窗口

[英]JQuery links to open new window

I'm trying to improve a music player i've created for my website by moving in from in-browser to a separate pop-up window, i've created/manipulated code i've found to make the jQuery take the href value and send it to a new window, only issue is on clicking the <a> tag, it performs the href click and jQuery action (as it would do). 我正在尝试通过从浏览器内移至单独的弹出窗口来改进为网站创建的音乐播放器,创建/操纵了代码,发现使jQuery具有href值并将其发送到新窗口,唯一的问题是单击<a>标记,它执行href单击和jQuery操作(将这样做)。 I'm trying to find an efficient way to make it so that if a user has JavaScript disabled, they can at least use the music player in a new tab rather than not being able to listen at all, but i'm unsure of how to go about this. 我正在尝试找到一种有效的制作方法,以便在用户禁用JavaScript的情况下,他们至少可以在新标签页中使用音乐播放器,而不是根本无法收听,但是我不确定如何去做这个。 Would setting the elements href into a var, then removing the href attribute work? 是否将元素href设置为var,然后删除href属性起作用? Or would this cause errors? 还是会导致错误?

Example HTML: HTML示例:

<a href="this_page.cfm?song=1" target="_blank" class="playSong" id="#artist# - #song#">Play Track</a>

Example jQuery: jQuery示例:

<script type="text/javascript">
    $(document).ready(function(){
        $(".playSong").click(function(){
            var url = $(this).attr('href');
            var windowName = $(this).attr('id');

            window.open(url, windowName, "height=300,width=400");
        });
    });
</script>

Alternatively use e.preventDefault() or return false 或者使用e.preventDefault()return false

$(document).ready(function(){
    $(".playSong").click(function(e){
        e.preventDefault(); // this will prevent the browser to redirect to the href
        // if js is disabled nothing should change and the link will work normally
        var url = $(this).attr('href');
        var windowName = $(this).attr('id');
        window.open(url, windowName, "height=300,width=400");
    });
});

Using a data-* attribute would be better than storing song and artist data in the ID. 使用data-*属性比在ID中存储歌曲和艺术家数据更好。

<a href="this_page.cfm?song=1" target="_blank" class="playSong" data-info="#artist# - #song#">Play Track</a>

.preventDefault() or return false; .preventDefault()return false; can be used to stop the browser from following the link if Javascript is enabled: 如果启用了Javascript,可用于阻止浏览器跟踪链接:

$(".playSong").click(function(){
    var url = $(this).attr('href');
    var windowName = $(this).data('info'); // <-- using .data()

    window.open(url, windowName, "height=300,width=400");
    return false;
});

You need to give return false upon clicking the link. 单击链接后,您需要给return false。 As follows : 如下 :

    $(document).ready(function(){
        $(".playSong").click(function(){
            var url = $(this).attr('href');
            var windowName = $(this).attr('id');

            window.open(url, windowName, "height=300,width=400");
            return false;
        });
    });

Here is the actual complete answer to your questions 这是您问题的实际完整答案

How to make the link work in a new window if javascript is disabled or a popup blocker is active 如果禁用了JavaScript或激活了弹出窗口阻止程序,如何使链接在新窗口中工作

$(function(){
  $(".playSong").on("click",function(e){
    var w = window.open(this.href, this.target, "height=300,width=400");
    if (w) e.preventDefault(); // prevent link but only if not blocked
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM