WordPress.org

Ready to get started?Download WordPress

Forums

Comment URL In New Window (19 posts)

  1. iselltheta
    Member
    Posted 2 years ago #

    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!

  2. haxxxton
    Member
    Posted 2 years ago #

    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');
    	});
    });
  3. iselltheta
    Member
    Posted 2 years ago #

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

  4. haxxxton
    Member
    Posted 2 years ago #

    :) 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

  5. iselltheta
    Member
    Posted 2 years ago #

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

  6. haxxxton
    Member
    Posted 2 years ago #

    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')

  7. iselltheta
    Member
    Posted 2 years ago #

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

  8. iselltheta
    Member
    Posted 2 years ago #

    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)...

  9. TalkingWithBees
    Member
    Posted 2 years ago #

    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

  10. capotea
    Member
    Posted 1 year ago #

    @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?

  11. haxxxton
    Member
    Posted 1 year ago #

    @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>
  12. capotea
    Member
    Posted 1 year ago #

    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/

  13. haxxxton
    Member
    Posted 1 year ago #

    @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)

  14. capotea
    Member
    Posted 1 year ago #

    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?

  15. haxxxton
    Member
    Posted 1 year ago #

    @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

  16. capotea
    Member
    Posted 1 year ago #

    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?

  17. capotea
    Member
    Posted 1 year ago #

    @haxxxton
    Sorry, I think I got it (after reading better what you said).
    First thanks very much for your help here, I appreciate that very much!

    The comment-author link now opens in a new tab as well.

    But still all the comment-section links open in two windows: the same and a new window.

  18. haxxxton
    Member
    Posted 1 year ago #

    @Fokkio

    you need to have a look at the change i made to the code above..

    you need to change where you have delegate('a','click',function(){ to delegate('a','click',function(e){

    this should stop it opening in the current window

  19. capotea
    Member
    Posted 1 year ago #

    @haxxxton: Super! You're the best! :-)
    Thanks for helping me out here!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.