Follow in Twitter & Facebook


Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools


Wednesday, August 15, 2012

Open hyperlink in new tab using jquery

In this article we will discuss how to open a hyperlink in new window or tab using jQuery or JavaScript. You can also check my last article on jQuery sample here. Also you can see some SharePoint articles, Jobs.

The javascript code to open hyperlink in new tab is below:

 <script type="text/javascript">
        $(function () {
            $("a").attr("target", "_blank");
        });
    </script>

This will open every links in a page in new window. But suppose you want it for a particular section then follow below code:
jQuery approach:

Add .js file to page as like below:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

Then write the script code like below:
<script type="text/javascript">
       $('.divclassname a').live('mouseover', function () {
           $(this).attr('target', '_blank');
       });
    </script>

<div class="divclassname">
<a href="http://www.sharepointdotnet.com" > SharePointDotNet</a>
</div>
<a href="http://www.fewlines4biju.com" > Fewlines4Biju</a>

Here in the above schenerious, only SharePointDotNet link will open in new tab or window but Fewlines4Biju will not open in new tab. Because we have mention the div class name in the jQuery function.




0 on: "Open hyperlink in new tab using jquery"