How can I open a link in a new window using css?

I need to make a bunch of links open in a new window - it's good that they all have the same css style - what do I need to do in css to make these links open in a new window?

+2


a source to share


3 answers


According to the comments:

and how can I specify the size of the window? I want it to be slightly smaller than the original page. also, can I make all the links on the page open in a new smaller window?

You cannot use CSS or HTML for this. You must use JavaScript window.open()

. You can get all the links element.getElementsByTagName()

to a

and you can determine the relationship <T23> attribute : element.className

window.onload = function() {
    var links = document.getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {
        var link = links[i];
        if (link.className == 'someClass') {
            link.onclick = function() {
                window.open(this.href, 'chooseYourName', 'width=600,height=400');
                return false;
            }
        }
    }  
} 

      



Or, if you are already using jQuery , you can use <T26> to select all links that the specified class has someClass

:

$(document).ready(function() {
    $('a.someClass').click(function() {
        window.open(this.href, 'chooseYourName', 'width=600,height=400');
        return false;
    });
});

      

The window name given in chooseYourName

will make sure all links are opened (re-opened) in the same window. You can also see that you can specify the width and height there.

+4


a source


You cannot use CSS for this. You need to use <a target="_blank"></a>

.



Edit: Javascript window.open command.

+1


a source


You cannot do this with CSS. You have to do it with a small script like:

<script type="text/javascript">
function newwindow()
{
    var load = window.open('http://www.domain.com');
}
</Script>

      

0


a source







All Articles