Search code examples
javascriptjqueryurl-parametersbusiness-catalyst

Passing 2 URL Parameters?


I need to pass 2 URL parameters in a URL. The URL originates in an email and the user will click the link directing them to my site. The first parameter triggers a script on the page the second parameter is for a module my CMS will render from the parameter.

First Parameter is : message=1 (This parameter triggers the javascript)

The second Parameter is: name={tag_recipientfirstname} (My CMS will render the module)

The script that is called for the first looks like this:

    <script type="text/javascript">
var $j = jQuery.noConflict();
$j(document).ready(function() {
var url = window.location.href;
url = url.toLowerCase();
if (url.indexOf('message=1') != -1) {
        $j("a.message").colorbox({
            open:true
});
   }
$j("a.message").colorbox(); //not related to URL parameter
}); 
</script>

The second parameter is used on the page as:

<div>
<p>{ module_url,name} (again CMS will render this module)</p>
</div>

EDIT

I realize I left a couple things out:

First: How do I pass both parameters so they will both function as listed above?

And the CMS I am using is Business Catalyst.


Solution

  • //split the `location.search` string at the ampersands
    var search_arr = window.location.search.replace('?', '').split('&'),
        len        = search_arr.length,
        get_vars   = {},
        tmp        = [];
    
    //iterate through the key/value pairs and add them to the `get_vars` object
    for (var i = 0; i < len; i++) {
        tmp = search_arr[i].split('=');
        get_vars[tmp[0]] = tmp[1];
    }
    
    //you can now access your GET variables through the `get_vars` object like: `get_vars.name`
    
    //you can check for the existence of a certain GET variable like this
    if (typeof(get_vars['message-1']) != 'undefined') {
        $j("a.message").colorbox({
            open:true
        });
    }
    

    Here is a demo:http://jsfiddle.net/aBH8K/1/ (http://jsfiddle.net/aBH8K/1/show/?message-1=3 to see with get var)

    Some related documentation: