Getting relative form actions with javascript

The other day I needed to get data from a form and manipulate the "get" manually through javascript. For that to happen I needed to obtain the action attribute of the form. The action of this specific form had to be a relative url. What I found through testing was that whether I used getAttribute('action') or action the relative url would sometimes become a fully qualified url depending on which method you used in which browser.

Method IE6 IE7 Firefox Opera Safari
getAttribute('action') Relative Relative Relative Fully Qualified Relative
action Relative Relative Fully Qualified Fully Qualified Relative

The end result of this little oddity is that you are best using fully qualified urls in form actions. However if you do need to use a relative url as an action you need to be careful to make sure that you end up with what you are expecting. For example I'd use getAttribute('action') and just fix the result coming from opera by checking for the presence of 'http'.

See the simple example

comments powered by Disqus