Open link in a new Window

Hi everyone,

I would like to add a new feature to my website. I have several links that are redirected when click over them to another page, but instead using <a> elements I am using “onclick” feature in a div.

I could use the following code in an <a> element to make it work:


But as I was saying, I am using the feature “onclick” in a <div>. Is there any way to do it this way?

Here is an example of what I have:

                            <?php foreach ($Blacks as $DWG): ?>

                                <div class="miniescaparate" onclick="location.href = 'single.php?ID=<?php echo $DWG['ID'] ?>'">
                                    <img src="caratulas/<?php echo $DWG['thumb'] ?>" alt="<?php echo 'Carátula para el juego ' . $DWG['titulo'] . ' de Xbox One' ?>" title ="<?php echo $DWG['titulo'] ?>"/>
                                    <p class="descuento">-<?php echo number_format($DWG['descuento_DWG'],0) ?>%</p>
                                    <p class="titulo"><?php echo $DWG['titulo'] ?></p>


                            <?php endforeach; ?>

Thanks in advice.

Is there a reason why you are using onclick rather than just using an <a> element?
If you were using html5 it is now valid to have block elements within an <a> tag. You can use css to make the <a> display as a block.

1 Like

Well the problem was that at the beginning I wasn’t going to add the feature, but I changed my mind and the easiest way I found was to add that “onclick” feature.

“onclick” won’t work for users who have JS disabled…


It may also cause problems for anyone using keyboard navigation.


So it´s better to modify the code and use <a> element?

Good choice :slight_smile:

1 Like

AS Everyone saying that we must use A tag instead of onclick because it will cause problem when javascript is disabled and it’s absolutely correct but still if you want do that using onclick then you can do it in following way:-

onclick="'The target page url', '_blank')"

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.