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 1 of 1
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    81
    Thanks
    0
    Thanked 8 Times in 8 Posts

    External JS File Argumet passing Object

    This object will let you read url arguments inside of an external js file.
    It allows you to branch code execution based on the argumets passed.

    An example script call could be:
    <script type="text/javascript" src="jsfilename.js?a=32&b=word"></script>


    Code:
    //-------------------------------------------------------
    // the ArgReader object code can be kept in an external 
    // file as long as its loaded before any ArgReader calls.
    
    var ArgReader=(function()
    {
        //used to hold the argument/value pairs
        var _scriptArgs=[];
    
        //parse the scripts url and fill scriptArgs with argumen/value pairs
        var _getScriptArgs=function() 
        {
            var url = document.getElementsByTagName("script")[document.getElementsByTagName("script").length-1].src;
            var v, args = url.split("?");
            if(args.length<2) return;
            var w = args[1].split("&");
            for(var i=0;i<w.length;i++)
            {
                v=w[i].split("=")
                _scriptArgs[v[0]]=v[1];
            }
        }
    
        //get the value associated with the argument (if any).
        var _getScriptArg=function(arg)
        {
            return _scriptArgs[arg];
        }
        return {
            setArgs:function(){_getScriptArgs();},
            getArg:function(n){return _getScriptArg(n);}
        }
    })();
    //-------------------------------------------------------
    
    
    // The following should be placed be in external file(s).
    //** USE: in any/all external js file(s) that you want to use passed arguments...
    //** eg script src: src="jsfilename.js?grgoo=crossbrowser&goo=yesitworks"
    
    //  * Required before any other ArgReader calls in each file you want to use arguments with...
    //  ArgReader.setArgs();
    
    //  * Get and use a named arguments value any way you desire...
    //  alert(ArgReader.getArg("goo"));

    While there is no real magic here (just normal DOM use to get the full url for the newly loaded file), there are 2 points to note:

    1) The document does not need to be completely loaded, enough of it will be loaded to allow querying its existing state.

    2) The new script object that is the newly loaded code will be last in the node list since it has just been loaded.

    var url = document.getElementsByTagName("script")[document.getElementsByTagName("script").length-1].src;


    The ArgReader Object is easy to use.
    Load it into the main page:
    <script type="text/javascript" src="ArgReader.js"></script>

    Call an external script:
    <script type="text/javascript" src="jsfilename.js?a=32&b=word"></script>

    In the external script:

    ArgReader.setArgs();
    var goo = ArgReader.getArg("b");



    Provided by rdspoons Aug 20 2010
    Last edited by rdspoons; 08-22-2010 at 08:36 AM.


 

Tags for this Thread

Posting Permissions

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