1

I have a javascript kind of feed, its more like a widget which displays dynamic content (that content is syndicated from other site). This is what I have:

<div id="previewWidget"></div>
<script type="text/javascript" src="https://api.something/hlwidgetcommon.js">
<script type="text/javascript" src="https://api.something/hlwidgetcommon.js"></script>
<script type="text/javascript" src="http://api.something/latestDiscussion.js"></script>
<script type="text/javascript">
  document.addEventListener('DOMContentLoaded', function() {
    hl.latestDiscussion('previewWidget', {
        discussionKey:'d06c3624-210e3-4a2b-a303-003f7ed66e038', <---------- random letters
        maxToRetrieve:'3',
        subjectLength:'50',
        contentLength:'160',
        moreUrl:'https://www.something.com',
        showLoginStatus:'0',
        loginUrl:'https://www.something.com',
        domainUrl:'https://www.something.com',
        cbUseBioBubble:'0',
        includeStaff:'1',
        HLIAMKey:'d06c3624-210e3-4a2b-a303-003f7ed66e038' <---------- more random letters
    });
  });
</script>

(http://www.pastebin.ca/3030247)

What I would like to do is that each link that feed generates is opened in a new tab. There is no <a href="#"> for me to go and add target=_blank.

Example: This is what the feed generates (its a forum feed): https://i.stack.imgur.com/p0kHJ.jpg

  • Can you give an example of what the dynamic content is? – rady Jun 17 '15 at 18:53
  • What i meant by "Dynamic Content" was that is constantly refreshing. To be more exactly, this is a Forum Feed, each time someone replys on that forum the feed updates – Laurenzanoster Jun 17 '15 at 19:01
  • I under stand that. What I mean is how is the html structured, how are the links structured? Are they fired from an onclick event in JS, are they actual anchor tags. There are several ways of doing this and with out knowing more, we can not help you. If you are using a service, we need to know the service. – rady Jun 17 '15 at 19:08
  • Oh im sorry, yes, are anchor tags. There isn't JavaScript on the generated html. – Laurenzanoster Jun 17 '15 at 19:13
  • In that case you could get the anchor tags and set the attribute to target="_blank" You don't need . In jquery= $("a").attr("target", "_blank"); – rady Jun 17 '15 at 19:18
  • Thanks Rady, so i should put this on my header right? – Laurenzanoster Jun 17 '15 at 20:17
  • I think you should do this when ever you get a chunk of these feed blocks back. If the DOMContentLoaded is called everytime you get one, do that in there – rady Jun 18 '15 at 02:11

1 Answers1

0

Probably the easiest solution: if you have any <a> elements within your feed, then you can easily give them target="_blank" via JS. If using jQuery, you might use this bit of JS, for instance:

$("#previewWidget a[href]").each(function() {
    $(this).attr("target","_blank");
});

If you are looking to scan the text of each post for URLs, then you could wrap those URLs with <a target="_blank"> elements. For more info, see this post, as it's a bit complicated to detect URLs accurately with JS.

Unfortunately, opening links in a new tab using Javascript is unreliable because this comes down to browser configuration issues - often you will be caught in a pop-up blocker. See this post for more detail.

Community
  • 1
  • 1
sqsinger
  • 500
  • 1
  • 3
  • 13
  • I tried the first option but i couldnt make it to work. this is what i did: Went to the header.php, in between and pasted: Is that what you meant? – Laurenzanoster Jun 17 '15 at 20:36
  • You are correct to include jQuery within . For this solution to work, you should run the above javascript code AFTER every time that new content is loaded into your feed. Wrap my JS in a function, and call that function either with an event listener or a direct callback. [JSFiddle](http://jsfiddle.net/x9g84mpy/) for example. – sqsinger Jun 18 '15 at 13:50