Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: Sir Osis of Liver - lokakuu 03, 2013, 05:19:55 IP

Otsikko: Adding onClick to main button array
Kirjoitti: Sir Osis of Liver - lokakuu 03, 2013, 05:19:55 IP

Tinkering with this (http://www.simplemachines.org/community/index.php?topic=512029.msg3618048#new), and curious if it's possible to do something similar in the main menu button array in Subs.php.  Tried adding this to the array -



     'custom' => 'onclick="return confirm(\'Are you sure?\');"',



but it's ignored.

Otsikko: Re: Adding onClick to main button array
Kirjoitti: Arantor - lokakuu 03, 2013, 05:27:15 IP
There is a way you can do it but you're not supposed to do it, really :P

If you adjust the target to be
'target' => 'http://example.com" onclick="return confirm(\'Are you sure?\');',

it should work. The surrounding " will be added normally to start the link and end the onclick.
Otsikko: Re: Adding onClick to main button array
Kirjoitti: Sir Osis of Liver - lokakuu 03, 2013, 05:41:36 IP

Tried this -



'target' => 'http://www.simplemachines.org" onclick="return confirm(\'Are you sure?\');',



Shows the confirm box, but opens new blank window when click 'OK'.

And why does it require a single quote before the url, and double quote after?

Otsikko: Re: Adding onClick to main button array
Kirjoitti: Sir Osis of Liver - lokakuu 03, 2013, 05:47:50 IP

Ok, it was missing a double quote at the end, now it opens the page in a new window, should be _self.



'target' => $scripturl . '?action=help" onclick="return confirm(\'Are you sure?\');"',



Otsikko: Re: Adding onClick to main button array
Kirjoitti: Arantor - lokakuu 03, 2013, 05:48:21 IP
 Bah, it's not *target*, it's the normal href attribute (target is for something else unrelated)

As for the mismatch of quotes, I did actually explain that. But I guess I can be more thorough.

When it's output, it's output as an <a href="{href goes here}"> link. You already have the surrounding ", what this is doing is exiting the " for the href, and starting a new one.

So: <a href="{href goes here}"> becomes:

<a href="http://example.com/" onclick="return confirm('Are you sure?');"> because of the quote handling.

And no, it wasn't missing a double quote.
Otsikko: Re: Adding onClick to main button array
Kirjoitti: Sir Osis of Liver - lokakuu 03, 2013, 05:55:12 IP
I'll try that.  Meanwhile, this works -



'help' => array(
'title' => $txt['help'],
'href' => $scripturl . '?action=help',
'target' => '_self" onclick="return confirm(\'Are you sure?\');"',
'show' => true,
'sub_buttons' => array(
),
),



Looks like it shouldn't, but .

Otsikko: Re: Adding onClick to main button array
Kirjoitti: Arantor - lokakuu 03, 2013, 05:56:29 IP
Except it adds an extra " into your code that shouldn't be there. Look at the result source, you should find you have an extra " in there (for the very reason I outlined)
Otsikko: Re: Adding onClick to main button array
Kirjoitti: Sir Osis of Liver - lokakuu 03, 2013, 06:01:43 IP

Works either way, so I took out the '' on the end.



'target' => '_self" onclick="return confirm(\'Are you sure?\');',



How to you put it in the 'href' field?

Otsikko: Re: Adding onClick to main button array
Kirjoitti: Arantor - lokakuu 03, 2013, 06:08:13 IP
Same way you do anything else...

'href' => $scripturl . '?action=help" onclick="return confirm(\'Are you sure?\');',
Otsikko: Re: Adding onClick to main button array
Kirjoitti: Sir Osis of Liver - lokakuu 04, 2013, 11:03:26 IP

Ok, much cleaner. Had tried that, but didn't get the quotes right.

Thx.