News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Image attachment click --> full size image opens up in a new window

Started by GigaWatt, January 03, 2021, 06:48:58 PM

Previous topic - Next topic

GigaWatt

I know this is the default behavior when you've got image thumbnails enabled, but is there an option that can enable this behavior, even if no image thumbnails are displayed?

Also, I've encountered this inconsistency. If you browse the attachments from the admin panel (Administration Center --> Attachments and Avatars --> Browse Files), this is the default behavior regardless of what the image thumbnail settings are: clic on a link for an image attachment --> a new window pops up and displays the image. So, what I would like to do is this to be the default behavior all over the forum, not just in the admin panel ;).
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

efk

I'm using mod, so not sure what is normal default behavior and want to know that. Since this is connected I'll add link with similar question  https://www.simplemachines.org/community/index.php?topic=576226.0

GigaWatt

Also, is there a way to open up the image in a new tab instead of a new window? I want the forum to be backwards compatible, but I haven't seen a browser in the last 10 years that didn't support tabs, and I seriously doubt someone would be using IE8 (I think it didn't support tabs ???) to browse the forum.
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

GL700Wing

Quote from: GigaWatt on January 03, 2021, 06:48:58 PM
I know this is the default behavior when you've got image thumbnails enabled, but is there an option that can enable this behavior, even if no image thumbnails are displayed?

Also, I've encountered this inconsistency. If you browse the attachments from the admin panel (Administration Center --> Attachments and Avatars --> Browse Files), this is the default behavior regardless of what the image thumbnail settings are: clic on a link for an image attachment --> a new window pops up and displays the image. So, what I would like to do is this to be the default behavior all over the forum, not just in the admin panel ;).

You could try the following:




The following code change will open an image attachment in a new tab/window when no image thumbnails are displayed.

In ./Sources/Display.php

Find:
if (!$attachmentData[$i]['is_image'])
continue;


Replace With:
if (!$attachmentData[$i]['is_image'])
{
if (!empty($attachment['width']) && !empty($attachment['height']))
$attachmentData[$i]['href'] = $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'] . ';image" target="_blank';

continue;
}





The following code changes will open an image in a new tab/window when the image thumbnail is clicked while still allowing the image to be downloaded when the link below the image thumbnail is clicked.

In ./Sources/Display.php

Find:
$attachmentData[$i]['thumbnail']['javascript'] = 'return expandThumb(' . $attachment['id_attach'] . ');';

Add After:

$attachmentData[$i]['thumbnail']['javascript'] = '';


In ./Themes/default/Display.template.php

Find:
if ($attachment['is_image'])
{
if ($attachment['thumbnail']['has_thumb'])
echo '
<a href="', $attachment['href'], ';image" id="link_', $attachment['id'], '" onclick="', $attachment['thumbnail']['javascript'], '"><img src="', $attachment['thumbnail']['href'], '" alt="" id="thumb_', $attachment['id'], '" /></a><br />';
else
echo '
<img src="' . $attachment['href'] . ';image" alt="" width="' . $attachment['width'] . '" height="' . $attachment['height'] . '"/><br />';
}

Replace With:
if ($attachment['is_image'])
{
if ($attachment['thumbnail']['has_thumb'])
echo '
<a href="', $attachment['href'], ';image" target="_blank" id="link_', $attachment['id'], '" onclick="', $attachment['thumbnail']['javascript'], '"><img src="', $attachment['thumbnail']['href'], '" alt="" id="thumb_', $attachment['id'], '" /></a><br />';
else
echo '
<img src="' . $attachment['href'] . ';image" target="_blank" alt="" width="' . $attachment['width'] . '" height="' . $attachment['height'] . '"/><br />';
}



Quote from: GigaWatt on January 09, 2021, 10:26:03 AM
Also, is there a way to open up the image in a new tab instead of a new window? I want the forum to be backwards compatible, but I haven't seen a browser in the last 10 years that didn't support tabs, and I seriously doubt someone would be using IE8 (I think it didn't support tabs ???) to browse the forum.
AFAIK when the  target="_blank"  attribute is not used your browser and the way it's configured determine this behaviour - by default some browsers open links in the same tab, some browsers open links in new tabs and some browsers open links in new windows.  Also, some browsers (eg, Chrome, Edge) do not have a setting to change the default behaviour of opening a link in the same tab and you either have to use Ctrl-Click to open a link in a new tab/window or install an add-on/extension.
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

GigaWatt

Quote from: GL700Wing on January 09, 2021, 09:25:05 PM
AFAIK when the  target="_blank"  attribute is not used your browser and the way it's configured determine this behaviour - by default some browsers open links in the same tab, some browsers open links in new tabs and some browsers open links in new windows.  Also, some browsers (eg, Chrome, Edge) do not have a setting to change the default behaviour of opening a link in the same tab and you either have to use Ctrl-Click to open a link in a new tab/window or install an add-on/extension.

Thanks for the input, I'll test it and see if it works ;).

Found this.

https://stackoverflow.com/questions/3970746/html-how-to-force-links-to-open-in-a-new-tab-not-new-window

Couldn't this line be written like this?

$attachmentData[$i]['href'] = $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'] . ';image" target="_blank';

$attachmentData[$i]['href'] = $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'] . ';image" target="_blank style=target-new: tab';
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

GigaWatt

Hm... coincidentally, someone else was looking for something similar, but for redirection boards and shadav shared this post from Antes.

https://www.simplemachines.org/community/index.php?msg=4056808

'target="_blank"' : ''

Apparently, this opens a new tab ???.
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

GL700Wing

Quote from: GigaWatt on January 10, 2021, 06:16:10 AM
Found this.

https://stackoverflow.com/questions/3970746/html-how-to-force-links-to-open-in-a-new-tab-not-new-window

Couldn't this line be written like this?

$attachmentData[$i]['href'] = $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'] . ';image" target="_blank';

$attachmentData[$i]['href'] = $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'] . ';image" target="_blank style=target-new: tab';
The stackoverflow info is more than 10 years old and it seems 'target-new' is not and, based on the information I've found, never has been supported by any browsers - see https://www.w3schools.com/cssref/css3_pr_target-new.asp.

Mostly SMF uses JavaScript for opening images and the browser determines whether they are opened in the same tab, a new tab or a new window and the only way I could find to always have an image open in a new tab is by using HTML.
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

GigaWatt

Quote from: GL700Wing on January 10, 2021, 03:24:45 PM
Mostly SMF uses JavaScript for opening images and the browser determines whether they are opened in the same tab, a new tab or a new window and the only way I could find to always have an image open in a new tab is by using HTML.

Mhm... so what you're saying is that when popups open in my browser, whether it's a new window or a new tab (I get both), in the new tab scenario, they're explicitly telling my browser to open the popup in a new tab via HTML?

What about this? I posted it in the post above the one you quoted. I mean... is there a chance it could work :)?

Quote from: GigaWatt on January 10, 2021, 06:39:16 AM
'target="_blank"' : ''
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

GL700Wing

Quote from: GigaWatt on January 10, 2021, 03:36:13 PM
Quote from: GL700Wing on January 10, 2021, 03:24:45 PM
Mostly SMF uses JavaScript for opening images and the browser determines whether they are opened in the same tab, a new tab or a new window and the only way I could find to always have an image open in a new tab is by using HTML.

Mhm... so what you're saying is that when popups open in my browser, whether it's a new window or a new tab (I get both), in the new tab scenario, they're explicitly telling my browser to open the popup in a new tab via HTML?
I'm getting a bit out of my depth here but AFAIK Javascript tells your browser to open links in a new window but some browsers (eg, Firefox) can be configured so that links are opened in a new tab instead.

Also, and from the testing I've done, if a link has both  'target="_blank"'  and an  'onclick'  setting that calls Javascript to open a new window the Javascript code takes precedence - that's why I had to add the  $attachmentData[$i]['thumbnail']['javascript'] = '';  line.

Quote
What about this? I posted it in the post above the one you quoted. I mean... is there a chance it could work :)?

Quote from: GigaWatt on January 10, 2021, 06:39:16 AM
'target="_blank"' : ''
I understand that the code is for a conditional test but I couldn't work out where you thought that could be used ...
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

GigaWatt

Quote from: GL700Wing on January 10, 2021, 07:11:46 PM
I understand that the code is for a conditional test but I couldn't work out where you thought that could be used ...

Instead of just plain target="_blank', I use 'target="_blank"' : '' ;).
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

GigaWatt

OK, I've implemented your modification and it seems to work :). Not only that, but it opens the image in a new tab in Pale Moon, Opera, Chrome and Firefox, no errors appear in the error log ;). Thank you :).

You should definitely open a topic about this in Tips & Tricks ;).
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

GL700Wing

Quote from: GigaWatt on January 10, 2021, 07:26:17 PM
Quote from: GL700Wing on January 10, 2021, 07:11:46 PM
I understand that the code is for a conditional test but I couldn't work out where you thought that could be used ...

Instead of just plain target="_blank', I use 'target="_blank"' : '' ;).

Is this what you mean?
$attachmentData[$i]['href'] = $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'] . ';image" target="_blank"' : '';



Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

GigaWatt

Quote from: GL700Wing on January 10, 2021, 10:49:33 PM
Is this what you mean?
$attachmentData[$i]['href'] = $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'] . ';image" target="_blank"' : '';

Yep ;).

Haven't tried it. Do you think it would work :)?

I made a mistake in my previous post, instead of "I use" should've been just "use". Sorry, I was tired :-\.

Quote from: GigaWatt on January 10, 2021, 07:26:17 PM
Instead of just plain target="_blank', I use 'target="_blank"' : '' ;).

Quote from: GigaWatt on January 10, 2021, 07:26:17 PM
Instead of just plain target="_blank', use 'target="_blank"' : '' ;).
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

Steve

DO NOT pm me for support!

GL700Wing

Quote from: GigaWatt on January 11, 2021, 09:00:53 AM
Quote from: GL700Wing on January 10, 2021, 10:49:33 PM
Is this what you mean?
$attachmentData[$i]['href'] = $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'] . ';image" target="_blank"' : '';

Yep ;).

Haven't tried it. Do you think it would work :)?
Nope - because there is no condition test in the that line of code - the condition test is in the 'if' statement on the previsions line.

If you wanted to combine those two lines of code (ie, the 'if' test and the variable definition) it would look like this:
$attachmentData[$i]['href'] = (!empty($attachment['width']) && !empty($attachment['height']) ? $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'] . ';image" target="_blank' : $attachmentData[$i]['href']);


Quote
I made a mistake in my previous post, instead of "I use" should've been just "use". Sorry, I was tired :-\.

Quote from: GigaWatt on January 10, 2021, 07:26:17 PM
Instead of just plain target="_blank', I use 'target="_blank"' : '' ;).

Quote from: GigaWatt on January 10, 2021, 07:26:17 PM
Instead of just plain target="_blank', use 'target="_blank"' : '' ;).
Ah - ok - I was wondering how/if that was working ...  ;)




Finally, I've just realised that you could use the following code for defining the  '$attachmentData[$i]['href']'  variable (it's a bit shorter/neater).
if (!empty($attachment['width']) && !empty($attachment['height']))
$attachmentData[$i]['href'] = $attachmentData[$i]['href'] . ';image" target="_blank';


Or if you wanted to combine the two lines of code (ie, the 'if' test and the variable definition) it would look like this:
$attachmentData[$i]['href'] = (!empty($attachment['width']) && !empty($attachment['height']) ? $attachmentData[$i]['href'] . ';image" target="_blank' : $attachmentData[$i]['href']);
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

GigaWatt

Quote from: Steve on January 11, 2021, 10:21:38 AM
Is this resolved then GW?

Yep :). Just gathering more info on the subject ;). Will mark as solved ;).

Quote from: GL700Wing on January 11, 2021, 04:03:32 PM
Nope - because there is no condition test in the that line of code - the condition test is in the 'if' statement on the previsions line.

If you wanted to combine those two lines of code (ie, the 'if' test and the variable definition) it would look like this:
$attachmentData[$i]['href'] = (!empty($attachment['width']) && !empty($attachment['height']) ? $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'] . ';image" target="_blank' : $attachmentData[$i]['href']);

Aha, got it ;).

No need to do that IMO, works fine the way it is now ;).

Quote from: GL700Wing on January 11, 2021, 04:03:32 PM
Finally, I've just realised that you could use the following code for defining the  '$attachmentData[$i]['href']'  variable (it's a bit shorter/neater).
if (!empty($attachment['width']) && !empty($attachment['height']))
$attachmentData[$i]['href'] = $attachmentData[$i]['href'] . ';image" target="_blank';


Or if you wanted to combine the two lines of code (ie, the 'if' test and the variable definition) it would look like this:
$attachmentData[$i]['href'] = (!empty($attachment['width']) && !empty($attachment['height']) ? $attachmentData[$i]['href'] . ';image" target="_blank' : $attachmentData[$i]['href']);

Yeah, much simpler ;). I've implemented the single line code and it works faster ;). Not really problem, it's not that big a deal if there's a few hundred ms delay, but still, there is a slight notable difference ;).

Right, marking this as solved ;).
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

Advertisement: