Welcome to TalkGraphics.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2009
    Location
    UK
    Posts
    472

    Default 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.)

  2. #2
    Join Date
    Jan 2004
    Posts
    1,830

    Default 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
    Attached Files Attached Files

  3. #3
    Join Date
    Feb 2009
    Location
    UK
    Posts
    472

    Default 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.
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2004
    Posts
    1,830

    Default 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
    Last edited by bb2; 28 January 2011 at 02:58 PM. Reason: adding info

  5. #5
    Join Date
    Jan 2004
    Posts
    1,830

    Default 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
    Attached Files Attached Files

  6. #6
    Join Date
    Feb 2009
    Location
    UK
    Posts
    472

    Default Re: custom javascript on button

    Thanks bb2 and John, I'd never heard of 'void' before!

 

 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •