Search code examples
phpechohref

PHP syntax for opening a new tab on click


I need to add target="_blank" property somewhere so that when logos are clicked they are opened in a new tab.

I am working on a website that uses the following PHP code to reference links in logos:

<?php if($cta): ?>
    <div class="product-logos-container">
        <?php foreach($cta as $_cta): ?>
            <span class="card" <?php if($_cta['banner_link']): ?>onclick="window.location.href='<?php echo $_cta['banner_link'] ?>'"<?php endif ?>>
                <span class="detail" style="opacity: 0;">
                    <span class="inner"><?php echo $_cta['banner_title'] ?></span>
                </span>
                <img src="/clear_cta/<?php echo $_cta['banner_filename'] ?>" alt="<?php echo $_cta['banner_title'] ?>" class="image"/>
            ......

Now I'm fairly certain that I need to add target="_blank" to the 4th line. I'm not sure which syntax is right for it, and after doing some research I'm starting to think that window.location might be the problem. If so please advise an alternative code that would open links when clicked in new tabs.


Solution

  • Use window.open instead of window.location

    window.open('<?php echo $_cta['banner_link'] ?>',
    '_blank' // <- This is what makes it open in a new window.
    );