custom javascript on button
Is it possible to have a button JUST run custom javascript? I can get the code to fire ok (by putting 'javascript: mycode...' in the 1st field in the 'Link' tab) but buttons appear to always assume you are intending to perform a navigation action, and it is this I don't know how to supress.
(I could use a placeholder and output my own button html but then I'd loose the convenience of simply dropping one of Xara's nice button designs on the screen.)
1 Attachment(s)
Re: custom javascript on button
Hi see the attached example.
I've created a head placeholder containing the function displaymessage()
To invoke this function, i set the link address of the Xara button to be javascript:displaymessage();
preview the page and click on the button to invoke the function.
Thanks
1 Attachment(s)
Re: custom javascript on button
Hi bb2, thanks for the quick reply. This is interesting... I've expanded on your example. Buttons '1' and '2' do the same thing (put text in the boxes next to them), the only diffence is '1' calls a function and '2' has the code inline. '2' messes up in that it tries to navigate away from the page - do you know why?
'3' and '4' are similar but have alert boxes and neither messes up - I'm guessing the alert box needs a 'click' which prevents the buttons default 'click' from firing.
Re: custom javascript on button
hi not sure im afraid. the same behaviour when created in a doc external to Xara programs.
the inline function test seems to work as expected in chrome but not in IE or FireFox.
If the entire function is created inline it also works but this seems messy. Perhaps someone else has some ideas and will reply accordingly..
To cause it to work correctly inline, i think youd have to apply it to an onclick event rather than within the href and i dont think this is possible from within the program
1 Attachment(s)
Re: custom javascript on button
Thanks to John for providing this tip.
The reason browser is trying to go somewhere after script execution is because the code actually returns something. To make sure any particular code does not return anything, simply add "void 0;" at the end. See the attached example, both links 1 and 2 now work as you require.
Thanks
Re: custom javascript on button
Thanks bb2 and John, I'd never heard of 'void' before!