0

I would like to get URL request variables from a link and pass them to a CFC component. I already have working code (jQuery, AJAX, CFC) that will handle everything, but I just need to grab #URL.whatever# from a particular link.

Within Coldfusion code I can easily do so with #URL.whatever# but have no idea how to get it from the client side. Also, does it matter if I have been using IIS URL rewrite? I am currently rewriting www.website.com/page.cfm?category=cat1 to www.website.com/page/cat1.

in both cases Coldfusion can access the request variable with #URL.category#, there is absolutely no difference. So how can I do this with JavaScript/jQuery, it shouldn't be complicated, right?

Ashley Strout
  • 6,107
  • 5
  • 25
  • 45
user1751287
  • 491
  • 9
  • 26

3 Answers3

0

Well, we'll need more details to suggest how to get a reference to the link, but something like this should work:

HTML

<a id="mylink" href="www.website.com/page.cfm?category=cat1">Website.com</a>

JS

var href = document.getElementById( 'mylink' ).href;
Kevin Boucher
  • 16,426
  • 3
  • 48
  • 55
0

Right, what you want to use is function to parse the window.location.href variable.

var URL_PARAM = getUrlVars()["category"];

Or, if the URL to your page was www.website.com/page.cfm?category=cat1&anotherparam=12345

var URL_PARAM1 = getUrlVars()["category"];
var URL_PARAM2 = getUrlVars()["anotherparam"];

I can't say for sure how it would operate with URL rewrites.

URLVars:

function getUrlVars() {
    var vars = {};
    var parts =window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[ decodeURIComponent(key)] = decodeURIComponent(value);
    });
    return vars;
}
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
Blaise Swanwick
  • 1,735
  • 1
  • 16
  • 18
0

This question proposes a method to get the variables, I find it slightly easier to understand than Blaise's regular expression. It also properly unencodes values from the URL Get Querystring with Dojo

function getUrlParams() {

  var paramMap = {};
  if (location.search.length == 0) {
    return paramMap;
  }
  var parts = location.search.substring(1).split("&");

  for (var i = 0; i < parts.length; i ++) {
    var component = parts[i].split("=");
    paramMap [decodeURIComponent(component[0])] = decodeURIComponent(component[1]);
  }
  return paramMap;
}

var params = getUrlParams();
console.log(params.myParam);
Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217