14

I am very new in javascript and jquery.

$.getJSON("idcheck.php?callback=?", { url:  /*i want full url to be print*/ }, function(json){
  //alert(json.message);
});

How do i get current full url on page on after url: in above?

Thank you

marcgg
  • 65,020
  • 52
  • 178
  • 231
  • I think this is a duplicate of http://stackoverflow.com/questions/406192/how-to-extract-current-url-in-jquery. – Dominic Rodger Nov 26 '09 at 10:47
  • I already seen it but i don't know how to implement it in my context, so any help would be greatful –  Nov 26 '09 at 10:49

4 Answers4

21

This will give you the current url:

window.location.pathname

edit:

$.getJSON("idcheck.php?callback=?", { url:  window.location.pathname }, function(json){
  //alert(json.message);
});

edit 2: Using PHP (found via)

<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
?>


$.getJSON("idcheck.php?callback=?", { url:  "<?php echo curPageURL(); ?>" }, function(json){
  //alert(json.message);
});
marcgg
  • 65,020
  • 52
  • 178
  • 231
  • How do i implement in it? Currently when i put this, it just shows as window.location.pathname; –  Nov 26 '09 at 10:47
  • it shows as url: window.location.pathname, not the current url of it, i need something like url : localhost/index.html –  Nov 26 '09 at 10:51
  • yes, that's the point. When it will be called it will use the correct url. If you want it to show in the source code of your page, use a backend language and not javascript – marcgg Nov 26 '09 at 10:55
  • I added an example to my answer – marcgg Nov 26 '09 at 10:58
  • 1
    Ah i see, sorry for the trouble caused. –  Nov 26 '09 at 10:58
5

You should use window.location.pathname or window.location

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
ikkebr
  • 791
  • 4
  • 11
3

To get current page URL via Jquery and Javascript

  $(document).ready(function() {
      //jquery
    $(location).attr('href');

    //pure javascript
    var pathname = window.location.pathname;

    // to show it in an alert window
    alert(window.location);
});


$.getJSON("idcheck.php?callback=?", { url:$(location).attr('href')}, function(json){
  //alert(json.message);
});
Mahendra Jella
  • 5,450
  • 1
  • 33
  • 38
3

You can use this:

var path = window.location.pathname; // path only
var url      = window.location.href;     // full URL

Edit:

$.getJSON("idcheck.php?callback=?", { url: window.location.href }, function(json){ alert(json.message);});
Mukul Keshari
  • 495
  • 2
  • 7