81058

Using forms and POST method to get URL parameters through Javascript

Question:

So let's say there's a URL <a href="http://example.com/index.html/hello/hi" rel="nofollow">http://example.com/index.html/hello/hi</a> where "hello" and "hi" are parameters.

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

Answer1:

Your subject is a little bit vague. However, I thought I'd made an example of possibilities. <a href="http://jsfiddle.net/tive/LjbPq/" rel="nofollow">http://jsfiddle.net/tive/LjbPq/</a>

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.

<ul><li><a href="http://www.w3schools.com/jsref/prop_doc_url.asp" rel="nofollow">documentation on document.URL</a> to retreive the complete URL</li> <li><a href="http://www.w3schools.com/jsref/obj_location.asp" rel="nofollow">documentation on window.location</a> to use properties of a url (protocol, href, pathname, ...)</li> </ul>

Answer2:

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];

Recommend

  • QMenu: Set text color for specific QAction
  • Finding regular expressions for languages otherwise described
  • Writing a recursive function on lists in Haskell
  • Is there a way to clone native functions in javascript like window.alert or document.write
  • Generating anchors with PyYAML.dump()?
  • cell spacing in div table
  • How can I tell a form not to dispose a particular control when it closes?
  • Hash Code in SQL Server?
  • Python ImageIO Gif Set Delay Between Frames
  • Invalid Date on validation Date of js
  • Why use database factory in asp.net mvc?
  • Angular2 component view does not update on value change via method
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • Read text file that is not in the main package in a runnable jar
  • Button text different than value submitted in query string
  • Odoo 10 - Overriding unlink method
  • How integrated is Collada to OpenGL ES
  • Debug.DrawLine not showing in the GameView
  • Parsing a CSV string while ignoring commas inside the individual columns
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • How to clear text inside text field when radio button is select
  • C++ Partial template specialization - design simplification
  • Repeat a vertical line on every page in Report Builder / SSRS
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Read text file and split every line in MSBuild
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • C# - Serializing and deserializing static member
  • Get object from AWS S3 as a stream
  • Java applet as stand-alone Windows application?
  • Updating server-side rendering client-side
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • Do I've to free mysql result after storing it?
  • Return words with double consecutive letters
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • Error creating VM instance in Google Compute Engine
  • using HTMLImports.whenReady not working in chrome
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection