Skip to content

Instantly share code, notes, and snippets.

@ekancepts
Created December 2, 2012 10:13
Show Gist options
  • Save ekancepts/4188044 to your computer and use it in GitHub Desktop.
Save ekancepts/4188044 to your computer and use it in GitHub Desktop.
Opening internal links in same window

##I am using wordpress 3.4.2
When used following code

<a href="<?php the_permalink(); ?> " target="_self">Test</a><br />
<a href="http://www.google.co.in/" >Google with http and no other attributes</a><br />
<a href="http://www.google.co.in/" target="_self">Google with http and target=_self</a><br />
<a href="google.com" >Google without http</a>

It is generating folowing HTML code

    <div id="links">
    <a href="http://myworks.eka/projects/local-dev/welthungerhilfe/?p=188 " target="_blank">This internal link</a><br>
    <a href="http://www.google.co.in/" target="_blank">Google with http and no other attributes</a><br>
    <a href="http://www.google.co.in/" target="_blank">Google with http and target=_self</a><br>
    <a href="google.com">Google without http</a>
    </div>

###How can i make all internal links open in same window and external links in new tab or window?

@ekancepts
Copy link
Author

Make appropriate changes in settings.js

$('a').each(function(index,value) {

if($(value).filter('[href$=".pdf"]').length || $(value).filter('[href$=".pdf"]').length)
    $(value).attr('target','_blank');

if($(value).filter('[href^="http://"]').length)
{ 
    if($(value).filter(':not([href^="YOUR site url"])').length) $(value).attr('target','_blank');
}

});

@ekancepts
Copy link
Author

Make appropriate changes in settings.js

$('a').each(function(index,value) {

if($(value).filter('[href$=".pdf"]').length || $(value).filter('[href$=".pdf"]').length)
    $(value).attr('target','_blank');

if($(value).filter('[href^="http://"]').length)
{ 
    if($(value).filter(':not([href^="YOUR site url"])').length) $(value).attr('target','_blank');
}

});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment