2009-11-26 10 views
9

Ich bin sehr neu in Javascript und Jquery.Erhalten der aktuellen URL mit Jquery

Wie bekomme ich aktuelle volle URL auf Seite auf nach URL: in oben?

Danke

+0

Ich denke, dies ist ein Duplikat von http://stackoverflow.com/questions/406192/how-to-extract-current-url-in-jquery. –

+0

Ich habe es schon gesehen, aber ich weiß nicht, wie ich es in meinem Kontext implementieren soll, also wäre jede Hilfe dankbar –

Antwort

13

Dies gibt Ihnen die aktuelle URL:

window.location.pathname 

edit:

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

bearbeiten 2: Verwendung von PHP (gefunden 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); 
}); 
+0

Wie implementiere ich es? Derzeit, wenn ich dies stelle, zeigt es nur als window.location.pathname; –

+0

Ich habe meine Nachricht bearbeitet, es sollte funktionieren – marcgg

+0

es zeigt als URL: window.location.Pfadname, nicht die aktuelle URL davon, ich brauche etwas wie URL: localhost/index.html –

4

Du solltest verwenden window.location.pathname oder window.location

2

Zur aktuellen Seite URL via JQuery und 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); 
}); 
0

Sie können diese erhalten verwenden:

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