Using forms and POST method to get URL parameters through Javascript

末鹿安然 提交于 2019-12-11 14:38:21

问题


So let's say there's a URL http://example.com/index.html/hello/hi where "hello" and "hi" are parameters.

How would you use javascript and forms method POST to extract the parameters?


回答1:


Your subject is a little bit vague. However, I thought I'd made an example of possibilities. http://jsfiddle.net/tive/LjbPq/

The idea is to split the URL for each character /, in whatever way you received it.

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

Since split() returns an array (zero based), you need to distract 1 from the total length to get the last index.

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

Run this in a for loop, and you should get the idea as occurring in the example.

  • documentation on document.URL to retreive the complete URL
  • documentation on window.location to use properties of a url (protocol, href, pathname, ...)



回答2:


this could work...

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


来源:https://stackoverflow.com/questions/11767140/using-forms-and-post-method-to-get-url-parameters-through-javascript

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