Reputation: 13527
Let's say I have a form located at:
http://www.mysite.com/some/place/some/where/index.php
And I have this form:
<form action="submit.php"> ... </form>
If I do this:
$("form").attr("action");
I will get:
submit.php
What is a foolproof way to get the action from the form, so that it includes the entire URL:
http://www.mysite.com/some/place/some/where/submit.php
?
Upvotes: 0
Views: 586
Reputation: 177885
Try
$("form").prop("action");
or (assuming your form is in the same folder as the page and the page URL ends with /something)
var loc = location.href;
var page = loc.substring(0,loc.lastIndexOf("/")+1)+
$("form").attr("action");
Handle query strings and hashes especially for Quentin:
var loc = location.origin + location.pathname;
var action = $("form").attr("action").split("/").pop();
var page = loc.substring(0,loc.lastIndexOf("/"))+"/"+action;
Upvotes: 3
Reputation: 391
var el = document.createElement('a');
el.href = $("form").prop("action");
alert(el.href);
Chrome adjusts the href to give the absolute url, not sure with other browsers
Upvotes: 0