如何从URL获取“foo”:http://www.loremipsum.com/mysite.html#foo [复制]

This question already has an answer here:

is there a way in javascript (jQuery) or PHP to get something after .html ?

I've tried in PHP $_SERVER['request_uri'] and JS window.location.pathname;

But it seems that everything is stripped away after .html

Anyone an idea how I could submit #foo after .html?

</div>

For javascript try this

hash = window.location.hash.substring(1);

But pay attantion this will not work in IE 7.

If you want to support IE7

hash = window.location.href.substr(location.href.indexOf("#"));

In Javascript you can get it using hash property:

window.location.hash

Can you try this,

in Javascript:

var query = location.href.split('#');
console.log(query[1]);

in PHP, You can use parse_url() if you supplied the mentioned full url , Since you have added php tag

$url="http://www.loremipsum.com/mysite.html#foo";
$urls = parse_url($url);        
$fragment = $urls['fragment'];

It makes it totally different whether you need to get is in the client [js] or in the server [php].

In the client, you just access it via window.location.hash.

In the server, mind that the fragment are never sent across HTTP, so you have to send it either as a querystring, or in the request body [if the HTTP method allows it].

Use window.location.hash instead of window.location.pathname.

This may help you:

<?php  
$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";

if ($_SERVER["SERVER_PORT"] != "80"){
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    $urls = parse_url($pageURL);        
    $fragment = $urls['fragment'];
    echo $fragment; //foo
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    $urls = parse_url($pageURL);        
    $fragment = $urls['fragment'];
    echo $fragment; //foo
}
?>