Hi, I'm making a 404 page, and I want it to display the current URL, but I don't want it to be server side. I don't know much about any variables are anything, so it would be nice if a nice person could give me all of the needed code and how to use it! :$
Thanks in advance!

Dani AI

Generated

Nice, 's quick answer is exactly the right idea and it fixed the immediate problem for . A couple of practical, production-minded improvements make the 404 page more reliable and safer:

  • Avoid using document.write on modern pages (it can overwrite the page if run after load and blocks parsing).
  • Put a small placeholder where the URL should go, then insert the address into that element after the DOM is ready, using textContent (not innerHTML) to avoid accidental HTML injection.
  • If you want a clickable URL, create an <a> element and set its textContent and href, and add rel="noopener noreferrer" if opening in a new tab.

Example (place the HTML where the URL should appear and run the script after the DOM is ready):

<p>Requested URL: <span id="url-placeholder"></span></p>

<script>
(function () {
  function showUrl() {
    var el = document.getElementById('url-placeholder');
    if (!el) return;
    el.textContent = window.location.href;
  }
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', showUrl);
  } else {
    showUrl();
  }
})();
</script>

Notes and troubleshooting:

  • If your server redirects visitors to a central /404.html, the browser address bar will show /404.html instead of the originally requested path. To preserve the original URL, serve the error page internally (for Apache use an ErrorDocument configured as an internal path rather than a full-URL redirect).
  • If you need the original requested path for logging or diagnostics, do that server-side — it’s more reliable than client-side reads.
  • Test edge cases (percent-encoded characters, long query strings, hash fragments) and ensure the placeholder exists before the script runs.

Recommended Answers

All 3 Replies

Why certainly!!

As gained from here:

<SCRIPT LANGUAGE="JavaScript">
      <!--
      document.write("The current URL is : "+location.href)
      // -->
</SCRIPT>

You will need to modify it a bit to match the input you want for your 404 but that snippet will get you the URL of the current page the user is on.

Hope that helps :)

Thank you SO MUCH!!!!
Solved.

Thank you SO MUCH!
Solved.
I have an other question about Javascript, but i'll post a new thread. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.