Getting current URL using Jquery

血红的双手。 提交于 2021-02-07 05:17:25

问题


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


回答1:


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);
});



回答2:


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




回答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);
});



回答4:


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);});


来源:https://stackoverflow.com/questions/1802954/getting-current-url-using-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!