'openl link in background tab and open modal in current tab

My code:

//html

<?php
$sql  = "SELECT * FROM table ORDER BY `id` DESC";
$stmt =$DBConnect->query($sql);
while ($row = $stmt->fetch()) { ?>

<a href="<?php echo $row['link']; ?>" class="link" data-toggle="modal" data-target="#<?php echo $row['id']; ?>" onclick="window.open(this.href, '_blank', 'location=yes,height=570,width=430,scrollbars=yes,status=yes');">

<?php include "modal.php"; } ?>

// modal.php

<div id="<?php echo $row['id']; ?>" class="modal fade code-modal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                ...
            </div>
            <div class="modal-body">
                ...
            </div>
        </div>
    </div>
</div>

In this code, modal popup in the same tab and the link <?php echo $row['link']; ?> open in new window.

My expected output is, the modal should open in the current tab and stay in the same tab and the link <?php echo $row['link']; ?> should open in background tab of the current window.

How can I do this?



Solution 1:[1]

You can add a new tag that will be hidden and that contains the link that should be opened in background:

<a href="your_link" target="_blank" class="hidden new_tab_link"></a>

And in js:

var myModalEl = document.getElementById('myModal')
myModalEl.addEventListener('show.bs.modal', function (event) {
  // do something like $('.new_tab_link').click()
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brayan Tiwa