Advertisement:

Author Topic: Open menu link in new tab?  (Read 2834 times)

Offline Sir Osis of Liver

  • SMF Hero
  • ******
  • Posts: 9,243
Open menu link in new tab?
« on: July 28, 2019, 11:16:42 PM »
Tinkering with adding a new menu button in 2.1 ACP to this support forum.  There are a couple of ways to make it work in same tab, but am unable to open new tab.  I've used redirectexit() and window.location, both open in same tab.  Different permutations of window.open are blocked as unrequested popups.  There's a link in Support and Credits that also loads in same tab.  Any way to load a menu link in new tab?

Online Antechinus

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 24,476
  • Master of BBC Abuse
Re: Open menu link in new tab?
« Reply #1 on: July 29, 2019, 05:21:39 PM »
Have you tried target="_blank"? That's the classic way of opening links in new tabs.

Code: [Select]
<a href="http:www.zomgwarezandpr0n.org" target="_blank" rel="noopener">Linky</a>
« Last Edit: July 29, 2019, 05:54:40 PM by Antechinus »

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,401
    • StoryBB/StoryBB on GitHub
Re: Open menu link in new tab?
« Reply #2 on: July 29, 2019, 05:45:31 PM »
It's also a security hole if you don't add rel="noopener" to the link.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Online Antechinus

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 24,476
  • Master of BBC Abuse
Re: Open menu link in new tab?
« Reply #3 on: July 29, 2019, 05:51:14 PM »
Oh yes? Handy tip. I haven't seen that mentioned anywhere, but I'll make sure I incorporate it in future. Have edited the code above.

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,401
    • StoryBB/StoryBB on GitHub
Re: Open menu link in new tab?
« Reply #4 on: July 29, 2019, 05:52:18 PM »
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline Sir Osis of Liver

  • SMF Hero
  • ******
  • Posts: 9,243
Re: Open menu link in new tab?
« Reply #5 on: July 29, 2019, 09:31:32 PM »
Have you tried target="_blank"? That's the classic way of opening links in new tabs.

Code: [Select]
<a href="http:www.zomgwarezandpr0n.org" target="_blank" rel="noopener">Linky</a>

Tried that already, doesn't work.  I'm trying to add it to the existing menu array, so it's consistent with core code -

Code: [Select]

'logs' => array(
'label' => $txt['logs'],
'function' => 'AdminLogs',
'icon' => 'logs',
'subsections' => array(
'errorlog' => array($txt['errorlog'], 'admin_forum', 'enabled' => !empty($modSettings['enableErrorLogging']), 'url' => $scripturl . '?action=admin;area=logs;sa=errorlog;desc'),
'adminlog' => array($txt['admin_log'], 'admin_forum', 'enabled' => !empty($modSettings['adminlog_enabled'])),
'modlog' => array($txt['moderation_log'], 'admin_forum', 'enabled' => !empty($modSettings['modlog_enabled'])),
'banlog' => array($txt['ban_log'], 'manage_bans'),
'spiderlog' => array($txt['spider_logs'], 'admin_forum', 'enabled' => !empty($modSettings['spider_mode'])),
'tasklog' => array($txt['scheduled_log'], 'admin_forum'),
'settings' => array($txt['log_settings'], 'admin_forum'),
),
),

/// add support button to SMF
'community' => array(
'label' => 'SMF Support',
'function' => 'Community',
'icon' => 'community',
),

'repairboards' => array(
'label' => $txt['admin_repair'],
'file' => 'RepairBoards.php',
'function' => 'RepairBoards',
'select' => 'maintain',
'hidden' => true,
),
),


Code: [Select]

/// this function adds link to community button
function Community()
{
redirectexit('https://www.simplemachines.org/community/index.php');  // opens in same tab
// echo '<script>window.location="https://www.simplemachines.org/community/index.php"</script>';  // opens in same tab
// echo 'window.open("https://www.simplemachines.org/community/index.php", "_blank")'; // doesn't work

echo '
//<a id="anchorID" href="https://www.simplemachines.org/community/index.php" target="_blank"></a>
//<script>document.getElementById("anchorID").click()</script>';  // popup blocked

}


Looks and works fine, but opens in same tab.  >:(


Offline Sir Osis of Liver

  • SMF Hero
  • ******
  • Posts: 9,243
Re: Open menu link in new tab?
« Reply #6 on: July 31, 2019, 11:53:45 AM »
Just getting back to this, nothing I've tried works.  I can scab on a separate menu button, but that's kind of crappy and causes problems in responsive view.  Any ideas?

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,401
    • StoryBB/StoryBB on GitHub
Re: Open menu link in new tab?
« Reply #7 on: July 31, 2019, 12:11:32 PM »
Where, exactly, are you trying to add this? Be specific, with screenshots, because this matters as to precisely how much code you need to add.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline Sir Osis of Liver

  • SMF Hero
  • ******
  • Posts: 9,243
Re: Open menu link in new tab?
« Reply #8 on: July 31, 2019, 12:19:23 PM »
I'm adding it to the menu array for the new cpanel style admin menu.  It works fine, but opens in same tab.

Edit:  replaced screenshot with larger view of modded ACP.  Gotta go.
« Last Edit: July 31, 2019, 12:38:11 PM by Sir Osis of Liver »

Offline Pipke

  • Full Member
  • ***
  • Posts: 474
    • [NRNS]
Re: Open menu link in new tab?
« Reply #9 on: July 31, 2019, 06:29:46 PM »
in Subs-Menu.php find:
Code: [Select]
// This is a shortcut for Font-Icon users so they don't have to re-do whole CSS.
$menu_context['sections'][$section_id]['areas'][$area_id]['plain_class'] = !empty($area['icon']) ? $area['icon'] : '';

add before:
Code: [Select]
// Open link in new tab
if (!empty($area['newtab']))
$menu_context['extra_parameters'] .= '" target="_blank" rel="noopener';

in Admin.php do this as your example:
Code: [Select]
/// add support button to SMF
'community' => array(
'label' => 'SMF Support',
'function' => 'Community',
'icon' => 'community',
'newtab' => true,
),




Offline Sir Osis of Liver

  • SMF Hero
  • ******
  • Posts: 9,243
Re: Open menu link in new tab?
« Reply #10 on: July 31, 2019, 10:56:47 PM »
Very nice, thanks Pipke.