2

I'm trying to figure out how to pass the URL of a current page via a hidden field so that I can redirect back to that page after the form's input has been handled. I've tried using javascript:location.href, however it looks as though it'll pass that as a literal string.

<input type="url" id="location" name="location" value="javascript:location.href" hidden />

When viewing the page source, I can see that the value of this input box is "javascript:location.href" rather than the page's URL. Any ideas here? Thanks in advance!

CodeMonkey13
  • 125
  • 2
  • 11

4 Answers4

3

You can access the element in Javascript and change the value there

document.getElementById('location').value = location.href;

Example: http://jsfiddle.net/6zxD5/

Robin Drexler
  • 4,307
  • 3
  • 25
  • 28
  • That would be possible, but you would need to put this script on the bottom of the page or it will get called before the DOM tree is ready. – Johann Bauer May 09 '14 at 20:01
  • yes, but I actually don't see this as a problem. Separating js from html tags isn't the worst thing to do, if you ask me. :) Unless you're doing angular :D – Robin Drexler May 09 '14 at 20:04
  • Okay, if you want that, `window.onload=function(){document.getElementById('location').value = location.href;}` would be even better. – Johann Bauer May 09 '14 at 20:06
  • One doesn't have to wait until window is loaded (including all images etc) in order to query the dom, right? – Robin Drexler May 09 '14 at 20:08
  • Well, it is the first callback on page load, so yes you have to. Look at this question: http://stackoverflow.com/a/14128765/1024057 – Johann Bauer May 09 '14 at 20:10
  • PTAL at https://developer.mozilla.org/en-US/docs/Web/Reference/Events/DOMContentLoaded and http://stackoverflow.com/questions/3698200/js-window-onload-vs-jquery-document-ready – Robin Drexler May 09 '14 at 20:11
  • I don't see the OP using jQuery. – Johann Bauer May 09 '14 at 20:14
  • `DOMContentLoaded` is only available in "modern" browsers. – Johann Bauer May 09 '14 at 20:15
  • 1
    which of the browsers do you consider "modern"? IE9? And we didn't even specify the constraints for our discussion. We're both right, ok ? :) – Robin Drexler May 09 '14 at 20:19
0

I don't think it works that way. You could just use the onload callback to insert it when the page is completely loaded:

<body onload="document.getElementById('location').value = document.location.href">
Johann Bauer
  • 2,488
  • 2
  • 26
  • 41
0

If your document containing the form is already a PHP file , you can do

 $yourPath = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

And in your html you would do

echo '<input type="url" id="location" name="location" value="'.$yourPath.'" hidden />';
Slytherin
  • 474
  • 3
  • 17
0

You can use JavaScript to set the value of the hidden field:

document.getElementById('location').value = window.location.href;

Example:

https://jsfiddle.net/moogs/mhjh0je3/

Since you also tagged PHP, here's the PHP version:

<input type="hidden" id="location" name="location" value="<?php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" />
Miguel Mota
  • 20,135
  • 5
  • 45
  • 64