Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript file not working on github pages [duplicate]

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");}
like image 729
Abhishek Kulshrestha Avatar asked Jul 10 '18 09:07

Abhishek Kulshrestha


People also ask

Does JavaScript working on GitHub Pages?

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.

Why my GitHub Pages is not working?

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.

Does GitHub Pages support multiple pages?

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.

How long does GitHub Pages take to refresh?

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."

How do you know if your website is failing on GitHub?

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.

Why do my links break when deploying to GitHub Pages?

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?

Where should I put my JavaScript files?

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.

How can you serve dynamic content on GitHub Pages?

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.


2 Answers

Try this

<script src="script.js" type="text/javascript"></script>

Use closing script tag.

For more check this answer.

like image 118
Durga Avatar answered Nov 14 '22 21:11

Durga


<script> tags need a closing tag even if it loads an external script.

You forgot it, so it tries to run </body></html> instead.

enter image description here

like image 20
Sebastian Speitel Avatar answered Nov 14 '22 22:11

Sebastian Speitel