Priya2026 Priya2026 - 1 year ago 42
Javascript Question

When I click on link it opens the two new tabs using jquery.This works in firefox but not working in chrome and IE

When I click on link it opens the two new tabs using jquery.This works in firefox but not working in chrome and IE.My Code is:



<script>
$(document).ready(function() {
$("#clickme").on("click", function(event) {
event.preventDefault();
window.open("http://localadvertisingthatworks.com?lp_redirect_4779=http://marketing.localadvertisingthatworks.com/acton/attachment/6817/f-001f/1/-/-/-/-/A%20guide%20to%20Local%20Advertising.pdf&amp;wp-cta-v=0&amp;wpl_id=TbfyVCtDihutDKsQUF8xD62LGQTWMJ369hN&amp;l_type=wpluid");
window.open("http://localadvertisingthatworks.com/download_thanks/");



});
});
</script>

<a class="inbound-button inbound-special-class" style="background-color: #2da8d2; border-color: #2da8d2; color: #fff; margin-right: 15px;" href="#" id="clickme" target="_blank"><i class="fa-download"></i>Download File</a>




Answer Source

Since chrome allows only one URL open per user action (clicking second time on your link will open second link) you must simulate opening (working example, just allow pop-ups, don't know why need that on chrome; can't use SO Snippet as it prevents new tab opening):

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    </head>
    <body>
        <a class="inbound-button inbound-special-class" style="background-color: #2da8d2; border-color: #2da8d2; color: #fff; margin-right: 15px;" href="#" id="clickme" target="_blank"><i class="fa-download"></i>Download File</a>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#clickme").on("click", function(event) {
                    event.preventDefault();

                    // random ID that will not match any other link on page.
                    var id1 = 'mrl-by-jj-'+Math.floor(Math.random() * 1000);
                    var id2 = 'mrl-by-jj-'+Math.floor(Math.random() * 1000);

                    // create links
                    var a1 = $('<a/>', {
                        href: 'http://localadvertisingthatworks.com?lp_redirect_4779=http://marketing.localadvertisingthatworks.com/acton/attachment/6817/f-001f/1/-/-/-/-/A%20guide%20to%20Local%20Advertising.pdf&amp;wp-cta-v=0&amp;wpl_id=TbfyVCtDihutDKsQUF8xD62LGQTWMJ369hN&amp;l_type=wpluid',
                        target: '_blank',
                        id: id1
                    });
                    var a2 = $('<a/>', {
                        href: 'http://localadvertisingthatworks.com/download_thanks/',
                        target: '_blank',
                        id: id2
                    });

                    // append to body
                    a1.appendTo('body');
                    a2.appendTo('body');

                    // trigger click and remove from DOM
                    $('#'+id1)[0].click();
                    $('#'+id2)[0].click();

                    $('#'+id1+', #'+id2).remove();
                });
            });
        </script>
    </body>
</html>