-2

I have a code to get number of shares of particular url. The url value is equal to the value we type in a text box.

Jsfiddle

Part of the code

html

 <input type="text" class="form-control" id="urlInput" />

Jquery

$('#getJSON').click(function () {
    $('#data-tab').fadeOut();
    $URL = $('#urlInput').val();

    totalShares($URL);

    $('#data-tab').fadeIn();

});

So , here $URL is equal to the value of text box.I need to change it so that $url changes to the value of current address bar url

I am bit new to jquery.I tried searching a lot. I tried replacing it document.url and location.href but it still doesn;t work.

Any tips? thanks

Edit:

I know the terminologies of the code console.log(window.location.href); ,window.location.href. however, when i use in the code, they don't seem to work

4
  • Hi @scrowler, i have already been through that...i have tried that method as written in the 2nd last line of the question, but in vain :(
    – Rahul Shah
    Commented Aug 7, 2014 at 4:18
  • to get the URL use document.URL or window.location.href.\
    – Mritunjay
    Commented Aug 7, 2014 at 4:18
  • Well document.URL will definitely get you the current URL. If it's not working, the actual URL could be something other than displayed in the browser URL bar. (Similar to how if you try to alert document.URL in a JSFiddle, the url's don't match exactly)
    – TrazeK
    Commented Aug 7, 2014 at 4:25
  • When you say "however, when i use in the code, they don't seem to work", can you explain what's not working exactly?
    – TrazeK
    Commented Aug 7, 2014 at 4:38

4 Answers 4

1

This has got to have been answered already, but have you tried.. console.log(window.location.href);? var url = document.URL? This isn't PHP, variables tend not to start with $

0
0

The url for the page your script is running on can be accessed by document.URL

0

You don't need jQuery for this. In JavaScript, it's like this:

var url = document.location;
0

To get Current Path:Use->

$(location).attr('href');

where location is an object.

You can get other properties of this objects from web browsers console window.

Not the answer you're looking for? Browse other questions tagged or ask your own question.