Tried to print the $_SERVER array in PHP, yet I can't find what I want:
http://www.domain.com/#sometaginpage
I want the "sometaginpage".
Help. Thanks!
You can get the full URL of the current page using the the window.location.href property. You can get the domain name from the URL of current page using the window.location.hostname property. You can get the pathname from the current URL a visitor is on using the window.location.pathname property.
This option requires enabling a hidden flag in Google Chrome. To find it, copy-paste the following text into Chrome’s address bar and press Enter: To the right of “Context menu show full URLs” on the Flags page, click the box and select “Enabled.” Click “Relaunch Chrome” to restart the browser.
How TO - Get Current URL With JavaScript ❮ PreviousNext ❯ Learn how to get the current URL with JavaScript. Current URL Use window.location.hrefto get the current URL address: Example document.getElementById("demo").innerHTML = "The full URL of this page is:<br>" + window.location.href; Try it Yourself »
Get the full uri with schema, host, port path and query. As mentioned in other answers, request.build_absolute_uri () is perfect if you have access to request, and sites framework is great as long as different URLs point to different databases. However, my use case was slightly different.
The browser doesn't actually send anything that comes after the hash(#) to the server because it is resolved within the browser.
Fairly certain that #hashtags
are NOT sent to the server, but you could develop a workaround with AJAX:
some-page.html:
<script type="text/javascript">
$(document).ready(function() {
$(window).bind('hashchange', function() {
var hash = window.location.hash.substring(1);
$.get('ajax-hash.php', { tag: hash },
function(data) { $('#tag').html(data); }
);
});
});
</script>
<div id="tag"></div>
<a href="#one">#one</a> | <a href="#two">#two</a> | <a href="#lolwut">#lolwut</a>
ajax-hash.php:
<?php
$hash = isset($_GET['tag']) ? $_GET['tag'] : 'none';
echo $_SERVER['HTTP_REFERER'] . '#' . $hash;
?>
Note: This is dependent on the browser actually sending the HTTP_REFERER
.. Since it's done through jQuery, it SHOULD.. but no promises! (Antivirus/Firewalls love to strip that from your packets)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With