External Links Open in New Window

Dynamically Added Links - External Links Open in New Window

Dynamically Added Links

by Jon_Hart » Thu Sep 29, 2016 10:32 pm

Would it be possible to have links that appear in new messages in Mchat activate the functionality of this extension? Right now it works on all links present when the page is loaded, but any new links that appear don't get changed until the page is reloaded again. Which is obviously more work than just right clicking the links in the first place.

Thanks for any help you can provide.
Jon_Hart
Registered User
Posts: 18
Joined: Tue Dec 29, 2015 10:03 am
Contact:

Re: Dynamically Added Links

by RMcGirr83 » Fri Sep 30, 2016 1:14 am

No as you found, the links are placed after the script from this extension has loaded (basically after the entire page has loaded). The author of the mchat extension should be able to use the functions of this extension.
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored
User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21034
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: Dynamically Added Links

by kasimi » Sat Oct 01, 2016 5:18 pm

There is no function for others to call because you use an anonymous function to change the links' onclick attribute. Even if there was one it wouldn't be as reasonable as handling dynamically added links in your extensions: let the click event bubble up and catch it at the document level. In addition to that you increase performance significantly: by leveraging event bubbling you only have one handler function instead of creating an onclick attribute for every link in the document.
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3789
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Dynamically Added Links

by RMcGirr83 » Sun Oct 02, 2016 11:47 am

Try this for the overall_footer_after.html file found in the styles/all/template/event folder of the elonw extension.

Code: Select all

<!-- IF S_ELONW -->
<script type="text/javascript">
var elonw_title = '{LA_ELONW}';
var source=removeSubdomain(location.hostname);
$(document).on('click',('a[href^="http://"], a[href^="https://"], a[href^="ftp://"]'), function() {
   var href = $(this).attr('href');
   var hostname = this.hostname;
   if(hostname && removeSubdomain(hostname)!==source)
   {
      window.open(href);
      return false;
   }
});
(function($) {
   $('.forum_link').each(function(){
      $(this).find('a').attr("onclick","window.open(this.href);return false;").attr("rel", "nofollow");
   });   
})(jQuery);
function removeSubdomain(a,b){return a.split(".").slice(-(b||2)).join(".")};
</script>
<!-- ENDIF -->


Ensure you purge the cache after making that change to that file.
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored
User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21034
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: Dynamically Added Links

by Jon_Hart » Thu Oct 13, 2016 8:20 pm

Ah cool, I installed this instead of the code I threw together. Works great and should be more resource efficient than what I did. Thanks!
Jon_Hart
Registered User
Posts: 18
Joined: Tue Dec 29, 2015 10:03 am
Contact: