Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Sir Osis of Liver on July 28, 2019, 11:16:42 PM

Title: Open menu link in new tab?
Post by: Sir Osis of Liver 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?
Title: Re: Open menu link in new tab?
Post by: Antechinus on July 29, 2019, 05:21:39 PM
Have you tried target="_blank"? That's the classic way of opening links in new tabs.

<a href="http:www.zomgwarezandpr0n.org" target="_blank" rel="noopener">Linky</a>
Title: Re: Open menu link in new tab?
Post by: Arantor on July 29, 2019, 05:45:31 PM
It's also a security hole if you don't add rel="noopener" to the link.
Title: Re: Open menu link in new tab?
Post by: Antechinus 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.
Title: Re: Open menu link in new tab?
Post by: Arantor on July 29, 2019, 05:52:18 PM
Oh yes. Have a read of https://mathiasbynens.github.io/rel-noopener/
Title: Re: Open menu link in new tab?
Post by: Sir Osis of Liver on July 29, 2019, 09:31:32 PM
Quote from: Antechinus on July 29, 2019, 05:21:39 PM
Have you tried target="_blank"? That's the classic way of opening links in new tabs.

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



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





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

Title: Re: Open menu link in new tab?
Post by: Sir Osis of Liver 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?
Title: Re: Open menu link in new tab?
Post by: Arantor 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.
Title: Re: Open menu link in new tab?
Post by: Sir Osis of Liver 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.
Title: Re: Open menu link in new tab?
Post by: Pipke on July 31, 2019, 06:29:46 PM
in Subs-Menu.php find:

// 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:

// 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:

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




Title: Re: Open menu link in new tab?
Post by: Sir Osis of Liver on July 31, 2019, 10:56:47 PM
Very nice, thanks Pipke. (https://www.simplemachines.org/community/proxy.php?request=http%3A%2F%2Fwww.thekrashsite.com%2Fpics%2Fthu.gif&hash=f4392e100b91a27ad55c137fafc83bec6aefd9cf)