chesterhow/tale

How to add script tag?

mmuratarat opened this issue · 7 comments

I am trying to create a customized 404.html page and add a Javascript script into it but it does not work. Could you help me how to a script to a post layout?

You can just include the script tag in your 404.html page. It should work just fine. If it still doesn't work maybe you could show me your source code and I can take a look for you :)

Thank you very much. That's very kind of you.

<script type="text/javascript">
        var errorMessages = ["I'll be back!",
                             'Hello, IT. Have you tried turning it off and on again?',
                             'Welcome to This Page. The first rule of This Page is: you do not talk about This Page.',
                             'Houston, we have a problem.',
                             "Error messages! Why'd it have to be error messages?!",
                             "I'm sorry, Dave. I'm afraid I can't do that.",
                             "You're gonna need a bigger server.",
                            "God help us; we're in the hands of engineers.",
                             "I'm as mad as hell, and I'm not going to take this anymore!",
                             "But we'll see each other soon. Won't we?",
                            "You're the wrong guy on the wrong page at the wrong time."
                            ];
      var item = errorMessages[Math.floor(Math.random() * errorMessages.length)];
      document.getElementById("error-message").innerHTML = "<p>" + item + "</p>";
      </script>

This is the script I want to use and I add it directly to my 404.html file but it does not do anything. I also tried this but to no avail.

Is your code repository hosted on GitHub? Maybe I could have a look. Can I also just check if your 404.html page works (even without the script)?

yes it is hosted on Github. It is working well without a script https://mmuratarat.github.io/404.html

Ok I cloned your repository and tested it out and I realised why it isn't working. You're using document.getElementById("error-message") in your code, but in your 404.html page there is no element with the id error-message.

A simple fix would be to just add this line in your 404.html file

<div id="error-message"></div>

Once you do that, you can add the script directly to 404.html and it'll work just fine.

Oh shoot! I completely forgot about that! Thank you very much!

No worries! Will be closing this then :)