ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to detect an external link dynamically?

December 15, 2010 11:59pm

Subscribe [3]
  • #1 / Dec 15, 2010 11:59pm

    roryrabbitfield

    320 posts

    So, I have an elaborate custom field set, with several of the custom fields containing info about URLs. The client will be able to enter links to URLs on our site, or to external URLs (off site). This is the code that takes one of the URL fields and displays a link around an image using the date supplied in the custom field:

    <a href="http://{promo_3_link}">{promo_3_image}</a>

    If the value of “promo_3_link” is a local URL, like:
    /home/mytemplate

    then I don’t want to do anything else with that link. But, if the value of “promo_3_link” starts with “http” I want to dynamically add code to it that would make that link open in an external window. How do I do this?

    Thanks.

  • #2 / Dec 16, 2010 12:09am

    Ray Brown

    33 posts

    You could give that link rel=“external” and then use JavaScript (or jQuery) to open all links with a rel of “external” in a new window.  This would save you the headache of trying to evaluate the URL in EE, and is also a fairly common way of managing links that open in new windows.

  • #3 / Dec 16, 2010 12:10am

    iain

    317 posts

    I use javascript for stuff like this,

    the code below basically checks links to see if they are pointing to an external domain, and if so it adds target blank to them

    jQuery("a[href^='http:']").not("[href*='yourdomain.com']").attr('target','_blank');

    you’d want to change “yourdomain.com” 😊

  • #4 / Dec 18, 2010 12:58pm

    roryrabbitfield

    320 posts

    I think I have this figured out; thanks to all who offered help.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases