I am able to load my javascript file on the local machine. But on github pages, it is not working.
I have included the file in index.html as
<script src="script.js" type="text/JavaScript"/>
The website is https://abhikulshrestha22.github.io./
The code is at https://github.com/abhikulshrestha22/abhikulshrestha22.github.io
Any help would be appreciated. Thanks in advance.
Edit 1 I tried to add the following code in my javascript file to check if its working, but it is not working on github page and working locally.
$(document).ready(function(){
alert("hi");}
GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files straight from a repository on GitHub, optionally runs the files through a build process, and publishes a website.
Solution: Verify the publishing source and published URL from your repository's settings tab. Double check that you are using the correct URL by visiting your repository's Settings, and scrolling down to the GitHub Pages section. You should see the URL where your site is published.
GitHub Pages' automatic generator only generates one page. You can add the new file directly, github page publishes all data (including data inside the subfolders) for you.
Note: It can take up to 10 minutes for changes to your site to publish after you push the changes to GitHub. If you don't see your GitHub Pages site changes reflected in your browser after an hour, see "About Jekyll build errors for GitHub Pages sites."
You see a blank page, or maybe the homepage shows up fine, but then you click on a link and nothing happens. You pop up dev inspector only to find a bunch of 404 status codes. It happens to many developers — the website works fine on localhost but after deploying to Github Pages, everything breaks.
To better understand why links often break when deploying to Github Pages, first we need to learn what root-relative links are. Root-relative links [1] are links that start with a forward slash ( / ). When clicked, a root-relative link ignores the path location of the current URL. Which part of an URL is the path?
I would advise you to move js files in a js directory on the root, following some best practices. Not the answer you're looking for? Browse other questions tagged javascript github github-pages or ask your own question.
If you have just started with GitHub pages you might be wondering how can you serve dynamic content. Well, you cannot really as the pages are static. However this should not stop you from writing in JavaScript and ending up with a Single Page Application . The first step in that direction is to see how to add JavaScript and jQuery to the pages.
Try this
<script src="script.js" type="text/javascript"></script>
Use closing script
tag.
For more check this answer.
<script>
tags need a closing tag even if it loads an external script.
You forgot it, so it tries to run </body></html>
instead.
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