Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2012
    Posts
    137
    Thanks
    1
    Thanked 1 Time in 1 Post

    How to have optional parameters in a function

    In the function below I would like to be able to pass an XML file to the SWF. I would like this to be optional, so that I can use this function with SWFs that do not have XML input. Is this possible?

    Code:
    function loadSwfToDiv(swf_url, div_id, ww, hh)
    {
    	var flashvars = {};
    	var params = {};
    	params.wmode = "transparent";
    	var attributes = {};
    	swfobject.embedSWF(swf_url, div_id, ww , hh, "9.0.0", false, flashvars, params, attributes);
    }

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts
    Which function argument determines whether or not the function will be executed?
    When "swf_url" is blank?

  • #3
    Regular Coder
    Join Date
    Jun 2012
    Posts
    137
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by jmrker View Post
    Which function argument determines whether or not the function will be executed?
    When "swf_url" is blank?
    The function is called from the html page. I would like to have the option to pass an XML file, OPTION.

    Code:
    <script type="text/javascript"> 
    loadSwfToDiv("c2/data/sld10.swf", "c2",600,500)
    </script> <div id="c2" />

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts
    I guess I don't know enough about 'swf' files nor XML file options to be of any help.

  • #5
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Code:
    function loadSwfToDiv(swf_url, div_id, ww, hh) {
    var swf_url_var = !swf_url ? code if argument empty : code if argument not empty ;
    Is this what you are asking? Basically, if an argument is not passed, default it; if it is passed, use it?
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,459
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Put your optional parameters last when you define your JavaScript functions.

    See http://javascriptexample.net/usefunction09.php for an example of how you then test if the optional value is supplied and set it to the default value if it isn't.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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