Current status of the Ning Platform is always available on the Ning Status Blog.

NC for Hire

Hi,

So here's a mini tip:

How to Make all links that leads out of your site open in a new tab.

That means , links that doesn't contain your network domain , will be opened in a  new tab. This method can be used so that your network doesn't lose precious traffic because of external links.

 

Normally if we do it using HTML , we will just add a target="_blank" attribute to it. What if user put an external in a comment , we can't edit the content.

<a href="externallink" target="_blank">External Site</a>

 

This is when the awesome jQuery steps in, just copy this code into your custom code:

<script type="text/javascript">
if (typeof(x$) != 'undefined') {
   //look at each links on your network
   //if link has http
   x$("a[href^=http]").each(function(){
     //if the link does not contain your hostname (yournetwork.com)
      if(this.href.indexOf(location.hostname) == -1) {
        //add attribute target to it to open in new tab
         x$(this).attr('target','_blank');
      }
   }); //end of each function
}
else{
}
</script>

 

I'm using jQuery's .each() and .attr() to get the work done.

Location.hostname is to get your domain name mynetwork.com

.indexOf is use to see if a string contains something.

== -1 That means does not exist! 

 

That's it. Hope you like it.

You need to be a member of Ning Creators Social Network to add comments!

Join Ning Creators Social Network

Votes: 0
Email me when people reply –

Replies

  • thanks, great script!

  • This is an important mini tip. Question, I have 2 websites meshed as 1 (Ning with a .php site).

    How would you write it so both sites stay on target?

    • NC for Hire

      Try this , replace otherdomain.com/file.php with your other site link:

      <script type="text/javascript">
      if (typeof(x$) != 'undefined') {
         //look at each links on your network
         //if link has http
         x$("a[href^=http]").each(function(){
           //if the link does not contain your hostname (yournetwork.com)
            if(this.href.indexOf(location.hostname) == -1 || this.href.indexOf('otherdomain.com/file.php') == -1) {
              //add attribute target to it to open in new tab
               x$(this).attr('target','_blank');
            }
         }); //end of each function
      }
      else{
      }
      </script>

  • This is a great tip thank you Elson :)

  • MINI??? MAXI!!! Thank you VERY much Elson

  • NC for Hire

    glad you all like it.

  • That worked AWESOME Elson!  Thanks!!!

  • Gracias me funciono de maravilla.

  • NC for Hire

    i covered this back in 2011 here but i like your solution much better!

    • NC for Hire
      Cool. I didn't know someone covered it when I posted. Sorry.
This reply was deleted.
 

Some interesting articles related to community management, digital marketing etc. could be found in our digest. Don't hesitate to leave a feedback so we would know that we should continue :-)

Latest Activity

Kos posted a discussion
In November we began to notice Chat "freezing up" and when we would refresh the page, it would…
Dec 27, 2024
Eva Libre posted a discussion
UpdateMerry ChristmasToday I woke up totally frustrated and determined to do something.At the end…
Dec 26, 2024
Eva Libre liked Eva Libre's discussion Ning 3.0 is antisocial!
Dec 25, 2024
Eva Libre updated their profile
Dec 25, 2024
Eva Libre replied to Eva Libre's discussion
Ning 3.0 is antisocial!
"Update
Merry Christmas
Today I woke up totally frustrated and determined to do something.
At the…"
Dec 25, 2024
Pam Givens replied to Pam Givens's discussion
Landing Page
"What I meant to say, if I could spell:
I've attempted to create a landing page and have't had much…"
Dec 12, 2024
Pam Givens posted a discussion
I've attempted to crate a landing page and have't had much success.  I'd rather have one on my…
Dec 12, 2024
Luxury Residences updated their profile
Dec 6, 2024
Asumi Carol updated their profile
Dec 2, 2024
Shada Hotel updated their profile
Nov 29, 2024
Howard Sands replied to Mark Thorpe's discussion
NING is broken?
"I am not a techie, I don't understand the first thing about it, but have a HUGE problem with Ning…"
Nov 27, 2024
Howard Sands replied to Donna MacShoe's discussion
Ning Support phone number for my 20 yr old site
"I hope this helps
Ning  https://www.ning.com/contact-us/
1-855-233-6436 (USA/Canada Toll Free)…"
Nov 27, 2024
More…

Meanwhile, you can check our social media channels