使用表单和POST方法通过Javascript获取URL参数

Using forms and POST method to get URL parameters through Javascript

本文关键字:Javascript 获取 URL 参数 方法 表单 POST      更新时间:2024-06-16

假设有一个URLhttp://example.com/index.html/hello/hi其中"hello"answers"hi"是参数。

您将如何使用javascript和表单方法POST来提取参数?

您的主题有点模糊。然而,我认为我已经做了一个可能性的例子。http://jsfiddle.net/tive/LjbPq/

这个想法是为每个字符/拆分URL,无论你收到它的方式是什么

var parts = document.URL.split("/");

由于split()返回一个数组(从零开始),因此需要从总长度中分散1的注意力才能获得最后一个索引。

var lastPart = parts[parts.length - 1];

在for循环中运行此操作,您应该会得到示例中出现的想法。

  • 文档上的文档。检索完整URL的URL
  • window.location上使用url属性的文档(协议、href、路径名…)

这可以工作。。。

var secondvar = window.location.href.split('/')[window.location.href.split('/').length];
var firstvar = window.location.href.split('/')[window.location.href.split('/').length-1];