WordPress.org

Support

Support » How-To and Troubleshooting » Comment URL In New Window

Comment URL In New Window

  • I was trying to figure out how to have a URL posted in a comment open in a new window. Right now the default is the current window and readers therefore are exited from my site. I was hoping to have the URL default to open in a new window. Does anyone know if this is possible and if so how to fix? Thank You!

Viewing 15 replies - 1 through 15 (of 18 total)
  • you could do it with jquery using something like

    $(window).ready(function(){
    	//adds target blank to all comment links
    	$('#comments a').each(function(){
    		$(this).attr('target','_blank');
    	});
    });

    Thank You for the speedy help. Curious does that go in my function.php file and if so where in the file? Thank You –

    🙂 nope,

    its javascript to best to put it in your footer.php

    paste all of this into your footer.php right before <?php wp_footer() ?>

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
    $(window).ready(function(){
    	//adds target blank to all comment links
    	$('#comments a').each(function(){
    		$(this).attr('target','_blank');
    	});
    });
    </script>

    You’re welcome

    You are the MAN! Worked like a charm. Thank you very much…

    keep in mind this will make ALL links in the comments section open in new windows..

    if you just want the ones people have posted in their actual written section change the section that says

    $('#comments a') to $('#comments .comment-body a')

    Excellent. Thank you again for your help. It is much appreciated.

    If you are still around I came across one little problem. I am using spectacu.la plugin which posts new comments via ajax. If someone posts a comment with a URL if I click that URL a new page does not open. But if I then hit the back key and reload the page and comment a new page does open after clicking the URL. Does this make sense that the ajax posted comment is not seeing the code you gave me in the footer and thus not opening a new page when I click on the URL. Yet if I reload the page the URL on the comment does then open a new page. Thank You (again)…

    Hi haxxxton,

    I tried your solution but in the first place I could not find <?php wp_footer() ?> in my footer.php.

    Graetful for any advice.

    Talking With Bees

    @haxxxton: your solution is not working for me unfortunately.
    I’m using the Atahualpa theme with WordPress 3.5.1

    Any idea how this might work?

    @Fokkio could you shoot me a link to your website please so i can see what’s loading when your page starts.. as that theme might not use #comments as the div id of the comments

    i hadn’t seen that there were new replies to this.. To handle the AJAX loaded I would adjust the script to look more like this:

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
    $(window).ready(function(){
    	//adds target blank to all comment links
    	$('#comments .comment-body').delegate('a','click',function(){
    		window.open( $(this).attr('href'), 'popup' );
                    e.preventDefault();
    	});
    });
    </script>

    Wow, thanks for your quick reply Haxxxton!
    This is my website http://blog.fokkio.nl/

    Links in the comment-section to test are here for example http://blog.fokkio.nl/bosfotos/

    @Fokkio
    yep as i suspected your container has a different ID.. your code would look like:

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
    $(window).ready(function(){
    	//adds target blank to all comment links
    	$('.comment-container p').delegate('a','click',function(e){
    		window.open( $(this).attr('href'), 'popup' );
                    e.preventDefault();
    	});
    });
    </script>

    EDIT: forgot the e in function(e)

    The link now opens in the same as well as in a new window 🙂
    Not totally good.

    Do you know code that opens the commenters profile links in a new window as well as the links mentioned in the comment?

    @Fokkio

    sorry that’s my fault i forgot to add ONE thing to the above code..
    the e in function(e)

    for peoples profile links as well change

    .comment-container p to .comment-container p, .comment-author

    No problem.
    I don’t know where to put that last line in the code exactly.
    Can you tell me after or between which point(s) it should be exactly?

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Comment URL In New Window’ is closed to new replies.
Skip to toolbar