Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a div as a link - option to open new tab?

Currently I'm using this small piece of js in my site to allow my div to act as a button:

<div id="item" onclick="location.href='http://www.google.com';" style="cursor:pointer;">Google</div>

But something I do very often when web browsing is opening a large amount of tabs. Is there any way I could modify my code to allow for this?

like image 693
Flaxbeard Avatar asked Sep 21 '12 11:09

Flaxbeard


People also ask

How do you make a link open a new tab?

How to Open Hyperlinks in a New Browser Tab or Window. The short answer is: just add a target="_blank" attribute to your links (anchor tags). Now when your visitors click that link, it will open in a new window or tab (depending on which web browser they are using and how they configured that browser).

Is there a way to open links in a new tab without switching to that tab?

You can load any link in a new browser tab by clicking or tapping on it while holding down the Control key (Windows) or the Command key (Mac).

How do you trigger the action of opening a link in a new window?

a target=”_blank” Open in New Browser Tab (or Window) The target attribute specifies where the linked document will open when the link is clicked. The default is the current window. If target="_blank" , the linked document will open in a new tab or (on older browsers) a new window.


2 Answers

This should do it:-

    <html>
    <head>
    <style>
    .sampleDiv
    {
            position:relative;
            margin:0px auto;
            width:400px;
            height:200px;
            background-color:#CCCCCC;
            text-align:center;
    }
    .actualLink
    {
            position:absolute;
            top:0px;
            left:0px;
            width:100%;
            height:100%;
    }
    .linkText
    {
            position:relative;
            top:80px;
    }
    </style>
    </head>
    <body>
            <div class="sampleDiv">
                    <a class="linkText" href="test.html">testing</a>
                    <a class="actualLink" href="test.html"></a>
            </div>
    </body>
    </html>

The link with class actualLink is covering whole of the div. The link with class linkText is providing the text. The purpose of using two tag is that if you only use actualLink then you cannot position the text wherever you want.By using the link with class linkText you have flexibility of centering(vertically) the text(horizontal centering can be done using only actualLink)

like image 188
Soumya Mukherji Avatar answered Oct 21 '22 10:10

Soumya Mukherji


My solution was to replace the div blocks with anchor blocks. Anchor blocks can now take on the CSS styles of nearly anything a div can do, but it can also include href, which the browser will recognize and give you the right-click options you want to see.

So old:

<div class="divClass" onClick="location.href='http://www.google.com'">asdf</div>

becomes

<a class="divClass" href="http://www.google.com">asdf</a>
like image 44
Junior Avatar answered Oct 21 '22 10:10

Junior