Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Retrieve and display address in address bar

amd.64

Coder
I am new to the forums, and although I have done web design in HTML, Java Script and CGI it has been awhile, about 20 years in fact. I have done very little with CSS. Please bear with me as I will likely have several questions. I do have extensive experience with Visual Basic, I have had Java and C# classes but that is where it ended. So I at least have the foundations for scripting and programming

I am working on creating a 404 file. I just about have it the way I want it, with the exception of one thing. I would like to retrieve the address in the address bar and display it on the 404 file. Kind of like:
Sorry the page you requested, wepage.html is not available.

So fair I currently have :
Javascript
Code:
<Script type="text/javascript">
        document.getElementById("file").innerHTML = window.location.pathname;
    </script>

HTML
Code:
<span id="file"></span>

However it is not working. Where have I gone wrong
 
This would probably be easier if you have access to apache or use .htacces in the root folder. As for JavaScript I think there is a back function you may be able to get the page from.
 
This would probably be easier if you have access to apache or use .htacces in the root folder. As for JavaScript I think there is a back function you may be able to get the page from.

I am running Apache on a self housed Ubuntu server. I created an .htaccess file however I am not sure it is working properly. I added lines to it to enable server side includes, to make my default index .shtml and 404 error page. However I didn't get any of them to work until I made changes in the config file. Unless the lines needed to made in both files. That is partly why I am making this web page - to learn.
 
Where have I gone wrong
I suspect that your script is working when the span id="file" doesn't exist yet. What if you try something like this:

HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>ERROR 404</title>
    <style>
    body{
    padding-top: 15%;
    text-align: center;
    }
    
    #file{
    font-weight: bold;
    }
    
    .pname{
    color: crimson;
    }
    </style>
   <script>
   onload = () => {
   document.getElementById('file').innerHTML = `Sorry the page you requested,<br><span class="pname">${window.location.pathname}</span><br>is not available.`;
   }
   </script>
  </head>
  <body>   
  <span id="file"></span>
  </body>
</html>
 
Awesome that is what I want. I wish it didn't have the / in front of the file name. But I can live with.

Thank You

As I said in my first comment it has been 20 years since I done this, and even back then I struggled with getting CGI and Java to play nice with HTML. I am still struggling with it.
 
Awesome that is what I want. I wish it didn't have the / in front of the file name. But I can live with.

Thank You

As I said in my first comment it has been 20 years since I done this, and even back then I struggled with getting CGI and Java to play nice with HTML. I am still struggling with it.
You could still remove that slash... with something like thus
JavaScript:
var fileName = filepath.replace('/', '');

That would replace the slash with an empty string
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom