abrabr
abrabr

Reputation: 217

get data from url

i took a ready script from here, How to read GET data from a URL using JavaScript? and can't make it work, what im doing wrong? here is my script:

function getUrlParam(param)
{
  param = param.replace(/([\[\](){}*?+^$.\\|])/g, "\\$1");
  var regex = new RegExp("[?&]" + param + "=([^&#]*)");
  var url   = decodeURIComponent(window.location.href);
  var match = regex.exec(url);
  return match ? match[1] : "";
}
var param = getUrlParam("process_number");
alert(param);

and here is my link:

http://erp.micae.com/index.cfm?fuseaction=objects.popup_print_files&process_number=SER-498&action_type=141&action_id=289&action_row_id=32&print_type=192

thx for the help!

Sorry guys, forgot to mantion that my page is working in a frame, that why it can't get the data from url i want :)

Upvotes: 1

Views: 5102

Answers (2)

user113716
user113716

Reputation: 322472

Since you're in a frame, if you need to get the href from the main window, do this:

var href = window.top.location.href;

Then process it.

Upvotes: 2

Pointy
Pointy

Reputation: 413712

That code has to run from the page whose URL you're mining for parameter values. In other words, it operates only on the current URL of the page it's on. It works fine.

If you want a function that gives you parameter values given an arbitrary URL, you'd just need to add an additional parameter:

function getUrlParam(param, url) {
  param = param.replace(/([\[\](){}*?+^$.\\|])/g, "\\$1");
  var regex = new RegExp("[?&]" + param + "=([^&#]*)");
  url   = url || decodeURIComponent(window.location.href);
  var match = regex.exec(url);
  return match ? match[1] : "";
}

alert(getUrlParam("process_number", "http://erp.micae.com/index.cfm?fuseaction=objects.popup_print_files&process_number=SER-498&action_type=141&action_id=289&action_row_id=32&print_type=192"));

Upvotes: 0

Related Questions