I'm trying to achieve something similar to what youtube has where when you press share button you can actually see url of current page inside some sort of text input and actually copy it etc. How can this be done with jQuery?
Asked
Active
Viewed 1,821 times
-5
-
6http://stackoverflow.com/questions/1034621/get-current-url-with-javascript searching faster than asking – RafH May 08 '13 at 20:49
-
He's not asking how to get the current URL (which even makes my answer wrong). He's asking how to get that into some input via jQuery. – Geeky Guy May 08 '13 at 20:59
-
1@Renan.. This could have helped him.. http://stackoverflow.com/questions/1222316/jquery-javascript-setting-the-attribute-value-of-a-textfield – writeToBhuwan May 08 '13 at 21:04
6 Answers
6
I think window.location.href would suffice.
Edit: as other have said:
$('#inputId').val(window.location);
Kudos for all the other guys who got that right before I did.

Geeky Guy
- 9,229
- 4
- 42
- 62
3
To get the path, you can use window.location.href:
var url = window.location.href;
$('#yor_text_id').val(url);

Mohamed Nagy
- 1,054
- 1
- 11
- 31
1
$("#YourTextInputID").val(window.location.href);

Adil Shaikh
- 44,509
- 17
- 89
- 111
-
this inserts page url inside #YourTextInputId correct? Or do I need to use window.location.href ? – Ilja May 08 '13 at 20:49
1
$("#input").val(window.location);
The jQuery way, though not needed.
$('input').val($(window).attr('location'));

writeToBhuwan
- 3,233
- 11
- 39
- 67
-
-
@Renan ... an object with a `.toString()` method set to return the href property... – bfavaretto May 08 '13 at 20:49
-