Search code examples
javascriptphpjqueryhtmljoomla

PHP How to open 2 links from one click "_self "and "_blank" different url's


So I was wondering how I can code a button to open 2 url's. I have seen retailmenot.com do it when clicking on the button "Get Deal". It opens a new tab with the their article inside their own website but on the original tab it's open a link to the deal site. eg. click on "Get Deal" here http://www.retailmenot.com/view/bestbuy.com

So my code I have done is

<!-- Author -->         
<span class="mix-author"></span>   
    <a attr="<?php echo $item->author_email; ?>" href="<?php echo $website_url; ?>" target="_blank" style="background-color: grey;padding: 5px 7px;border-bottom: 4px ;border-radius: 4px;float: right;color: white;" >GET DEAL</a> 
<?php } ?>

The above code opens in a new tab. but I want it to open in the current tab while I want the code(link) below to open in the new tab without showing me the title when the above "GET DEAL" is clicked.

<?php if ($params->get('show_title')) { ?>
    <!-- Title -->
    <h3>
        <a href="<?php echo $item->link; ?>" class="mix-title"><?php echo $item->main_title; ?></a> 
    </h3>
<?php } ?>

Please help me out.


Solution

  • I think you need to write a javascript function that does both and gets called on the button click:

    function twoLinks(same_page_url, new_window_url)
    {
        window.location = same_page_url; // this will open in the current window
        window.open(new_window_url); // this will open in a new window
    }
    

    and then call it like so from your link:

    <a attr="<?php echo $item->author_email; ?>" href="javascript:void(0);" onclick="twoLinks('<?php echo $website_url; ?>', '<?php echo $new_window_url; ?>');" style="background-color: grey;padding: 5px 7px;border-bottom: 4px ;border-radius: 4px;float: right;color: white;" >GET DEAL</a> 
    

    I borrowed from this answer for part of mine:
    jQuery: go to URL with target="_blank"

    Regarding your further comments, everything works for me as expected when I try your code. Inspect the html in the browser, and your GET DEAL link should contain something like:

    <a attr="" href="javascript:void(0);" onclick="twoLinks('http://www.arg.is', 'http://www.google.com');" style="background-color: grey;padding: 5px 7px;border-bottom: 4px ;border-radius: 4px;float: right;color: white;">GET DEAL</a> 
    

    I need to see error messages and/or the rendered html of your link to help you. I'm thinking either:

    1. You aren't included the http:// part like I have, and your link is trying a relative path that doesn't exist on your server
    2. One of your variables you're printing into the function parameters isn't what it should be