1

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?

caramba
  • 21,963
  • 19
  • 86
  • 127

6 Answers6

3

In Javascript you can get it using hash property:

window.location.hash
antyrat
  • 27,479
  • 9
  • 75
  • 76
3

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("#"));
0

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'];
Krish R
  • 22,583
  • 7
  • 50
  • 59
  • 1
    I would like a PHP way, but I need to get the url from the browser. and that's where "#foo" gets lost. – caramba Jan 30 '14 at 12:50
  • The example given above works perfectly – Grant Jan 30 '14 at 12:52
  • @Grant how will it work if I need something like $url=$_SERVER['request_uri']; if "#foo" is nowhere in a var_dump($_SERVER)? – caramba Jan 30 '14 at 12:54
  • $url = $_SERVER['REQUEST_URI']; Obviously the URL must contain #foo at the end. – Grant Jan 30 '14 at 12:56
  • @Grant ok I wrote "request_uri" not in uppercase. but it want work. try it... – caramba Jan 30 '14 at 12:57
  • $url = $_SERVER['REQUEST_URI']; $urls = parse_url($url); $fragment = $urls['fragment']; echo $fragment; Then run the file from your browswer appending #foo after the file name – Grant Jan 30 '14 at 12:58
0

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].

moonwave99
  • 21,957
  • 3
  • 43
  • 64
  • could you please explain how could I send it with querystring or in the request body? How do I see if HTTP method allows it? – caramba Jan 30 '14 at 12:52
  • @caramba you should depict your goal and you scenario. Answering the second question, [this question](http://stackoverflow.com/questions/978061/http-get-with-request-body) is relevant enough. – moonwave99 Jan 30 '14 at 12:58
0

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

MikaelE
  • 85
  • 7
0

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
}
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268