I just tried to reproduce the solution in the current version of xwiki. Unfortunately, (parts of ?) the code is just shown at the top of the wiki and does not solve the problem anymore
Youāll have to be more specific about what you actually did so we can figure out where it is going wrong for you.
The above solution continues to work for me.
Specifically, I implemented the following under Administration, Look & Feel, Presentation, HTTP Meta Info:
<script>
function fixExternalLinks() {
document.querySelectorAll('.wikiexternallink a').forEach(function(link) {
if (link.hasAttribute('target') == false) {
link.setAttribute('target', '_blank');
}
})
}
document.addEventListener("DOMContentLoaded", fixExternalLinks);
</script>
Basically, the javascript code code executes once the page is loaded, looks for any external links and if they do not already have a target specified, gives it a _blank target.
we just set up a wiki and using your solution. it works very good - thank you! we made an app as a database with infos about locations and their websites. on pages the code works perfect but on the app page - the overview with all entries it doesnāt work. the class on both pages are the same but i have no idea why itās not working there. may i ask you if you know why and if you have a solution?
The javascript code I loaded depends on knowing when the page is loaded and then the code looks through the links on the page and changes the target for the external links. Notice I said āexternal linksā. I identify the external links by the css css selector ā.wikiexternallink aā which means look for the wikiexternallink class, then the A target.
You can see this if you inspect element on your web page
However, as you said, the wiki external links inside the application do not get modified.
Iām guessing that the reason for this is, that the links inside the application are generated/created after the DOMContentLoaded event has already happened so the links donāt get the target modification.
How can you test this? After your application, put in a single external link anywhere on that page that is outside of the application provided links and see if that link has the ātargetā value assigned to it.
If that works, then that proves to me what the problem is.
If this turns out to be the problem, how do you fix it? At this point, I have no idea. Iāll have to play with my instance and see if I can work out an answer.
First thank you so much for answering so fast in this old thread. yes, its like you said. i created an external testlink and it opens in a new tab so i think the table is loaded after DOM ā¦ as you said. if you or someone will find a solution some time it would be super helpful. iām not a coder - i can read and understand some code but i can not write it.
Thank you very much for taking the time. Yes, it is a difficult task. Weāre not getting anywhere either.
I hope we get the feature target blank in the WYSIWYG editor when you build a link sometime in the future. That would be a solution to write the attribute directly to the link. But no matter how I try it, the attribute doesnāt work. If I try it directly in the editor, I can even see the attribute in the table next to the link, but without a function as plain text.
If we do get the problem solved, I will post the solution here.