JavaScript Feed-在新标签页中打开链接

时间:2022-12-09 20:16:15

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:

我有一种javascript类型的feed,它更像是一个显示动态内容的小部件(该内容与其他站点联合)。这就是我所拥有的:

<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)

(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.

我想要做的是,Feed生成的每个链接都在新选项卡中打开。我没有去添加target = _blank。

Example: This is what the feed generates (its a forum feed): http://imgur.com/iQ45OtK

示例:这是Feed生成的内容(论坛Feed):http://imgur.com/iQ45OtK

1 个解决方案

#1


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:

可能是最简单的解决方案:如果你的Feed中有任何元素,那么你可以通过JS轻松地给它们target =“_ blank”。如果使用jQuery,你可以使用这个JS,例如:

$("#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.

如果您要扫描每个帖子的URL文本,那么您可以使用元素包装这些URL。有关详细信息,请参阅此文章,因为使用JS准确检测URL有点复杂。

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.

不幸的是,使用Javascript在新选项卡中打开链接是不可靠的,因为这归结为浏览器配置问题 - 通常您会遇到弹出窗口阻止程序。有关详细信息,请参阅此帖子。

#1


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:

可能是最简单的解决方案:如果你的Feed中有任何元素,那么你可以通过JS轻松地给它们target =“_ blank”。如果使用jQuery,你可以使用这个JS,例如:

$("#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.

如果您要扫描每个帖子的URL文本,那么您可以使用元素包装这些URL。有关详细信息,请参阅此文章,因为使用JS准确检测URL有点复杂。

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.

不幸的是,使用Javascript在新选项卡中打开链接是不可靠的,因为这归结为浏览器配置问题 - 通常您会遇到弹出窗口阻止程序。有关详细信息,请参阅此帖子。