Basem
Basem

Reputation: 614

Relative form action resolves to absolute URL?

I am trying to get the actual string that is placed in a form action. The problem is when I do this, the action property resolves to an absolute path even though a relative path is in the HTML. How do I get the actual string that is in the action property?

Here is a sample of what I am referring to: http://jsfiddle.net/MSY4s/

Upvotes: 4

Views: 8487

Answers (3)

Paul Richter
Paul Richter

Reputation: 11072

If you're already using jquery, I would use the .attr function rather than extracting the DOM element from the jQuery object. Like so:

$("form").attr("action");

That should give you literally what is in the action attribute. In the example you provided, that should look like "/somewhere". The second example in your jFiddle will show a full path since that's what is in the action attribute.

Upvotes: 9

War10ck
War10ck

Reputation: 12508

Try this:

Give both of your forms ids:

<form id="form1" action="/somewhere" method="post">
<input type="text" name="test" />
</form>

<form id="form2" action="https://fiddle.jshell.net/somewhere2" method="post">
<input type="text" name="test" />
</form>​

Then using these ids you can get the action attribute of each form:

$('#form1').attr('action');

$('#form2').attr('action');

You can also set the action attributes using the same tags:

$('#form1').attr('action', '[New Action Value]');

$('#form2').attr('action', '[New Action Value]');

Hope this helps.

Upvotes: 1

Harsha Venkataramu
Harsha Venkataramu

Reputation: 2904

Relative URLs are always resolved to absolute ones on the base of the current document’s URL.

Upvotes: 1

Related Questions