get data from url

空扰寡人 提交于 2019-12-24 09:28:01

问题


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


回答1:


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.




回答2:


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"));


来源:https://stackoverflow.com/questions/5194280/get-data-from-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!