1

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 :)

Community
  • 1
  • 1
abrabr
  • 217
  • 5
  • 14
  • her eis the improved version http://stackoverflow.com/questions/901115/get-querystring-values-with-jquery/5158301#5158301 – KJYe.Name Mar 04 '11 at 13:26
  • That code works perfectly. What is the problem? – Pointy Mar 04 '11 at 13:30
  • 2
    You forgot to tell us **what is the expected result** and **what is the actual result**. *can't make it work, what im doing wrong* is not an error description. Do you expect us to *guess*? – Felix Kling Mar 04 '11 at 13:30
  • @Felix Kling i need to take data after "process_number" in this case SER-498 and show it in an opened page, that's all i want – abrabr Mar 04 '11 at 13:33
  • 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 :) – abrabr Mar 04 '11 at 13:52

2 Answers2

2

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.

user113716
  • 318,772
  • 63
  • 451
  • 440
0

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"));
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • thx but, it will work just for one page, the process number and action type and etc are variables that will change... – abrabr Mar 04 '11 at 13:36