javascripthtmlsrcipfsjs-ipfs

With Inline script working, How can I make my external .js file work properly with my HTML file?


I'm currently working on a website utilizing ipfs & pinning services like Pinata. My last build works flawlessly when delivered locally or through a public gateway. When trying to pin my last build to Pinata, I was informed I can't use inline JS. So my next build includes an external JS file called app.js. I'm storing this file within the same folder as my HTML file (index.html) and uploading to IPFS for testing purposes before pinning on Pinata. I cannot for the life of me get this new build to work properly. No matter how I try to call the .js filepath, I either get 400s or 500s every single time.

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

This is the line im using to call the file. I'm learning as I go so I don't know enough about permissions or other issues that may be causing this .js file to be unretreivable. At this point the code base is still pretty simple and I'm sure I'm missing something very obvious and just overlooking. Any help would Be appreciated!

Last build (Inline Script) example: LootCache.app

Ive tried (With the new build) every file path I can think of and nothing seems to work. I believe the JS code itself works because I simply used the last builds and externalized it. I also know atleast most of the HTML is working since the page populates. The only thing that doesn't work is the button & the returned data when the button is pressed.

HTML :

    <!DOCTYPE html>
<html>
<head>
    <title>LootCache</title>
    <script type="text/javascript" src="app.js"></script>

    <style>
        body {
            background-color: black;
            margin: 0;
            padding: 0;
        }

        .container {
            max-width: 100%;
            margin: 0 auto;
            border: 5px solid gold;
            padding: 10px;
            box-sizing: border-box;
            display: flex;
            flex-direction: column;
            align-items: center;
            height: 100vh;
            overflow: auto;
        }

        h1 {
            font-size: 50px;
            font-weight: bold;
            color: gold;
            margin: 10px 0;
            display: flex;
            align-items: center;
        }

        .input-container {
            background-color: white;
            padding: 10px;
            margin: 10px 0;
            border-radius: 5px;
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 100%;
            box-sizing: border-box;
            max-width: 800px;
        }

        input[type=text] {
            width: 100%;
            padding: 10px;
            border: none;
            border-radius: 5px;
            box-sizing: border-box;
            font-size: 16px;
            margin-bottom: 10px;
        }

        button {
            background-color: gold;
            color: black;
            border: none;
            border-radius: 5px;
            padding: 10px;
            font-size: 16px;
            cursor: pointer;
        }

            .response {
            color: gold;
            font-size: 20px;
            margin-top: 10px;
            word-wrap: break-word;
            text-align: center;
        }

        img {
            display: block;
            max-height: calc(100vh - 300px);
            margin: 10px auto;
            object-fit: contain;
            max-width: 100%;
            height: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <img src="https://ipfs.io/ipfs/QmXjtBuhfDRZfTXZjF2dpJ4mSnmjj5RPow4qMY5X6ZQ6QU?filename=LootCache-1.png" alt="LootCache">
        <h1></h1>
        <div class="input-container">
            <label for="ethAddress">Enter Ethereum Address (No ENS):</label>
            <input type="text" id="ethAddress" name="ethAddress" placeholder="0x...">
            <button id="checkBalanceBtn">Check Your Stash!</button>
        </div>
        <div class="response" id="response"></div>
    </div>
</body>
</html>

JAVASCRIPT:

window.onload = function() {
async function getBalance(address) {
    const response = await fetch(`https://mainnet.infura.io/v3/fb3bba54ae0449c28400a4e28fb61e6c?module=account&action=balance&address=${address}`);
    const data = await response.json();
    return data.result;
}

function displayAddress() {
    var address = document.getElementById("ethAddress").value;
    var provider = new Web3.providers.HttpProvider("https://mainnet.infura.io/v3/fb3bba54ae0449c28400a4e28fb61e6c");
    var web3 = new Web3(provider);
    web3.eth.getBalance(address, function(error, balance) {
        if (error) {
            document.getElementById("response").innerHTML = "Error: " + error.message;
        } else {
            var balanceInEther = web3.utils.fromWei(balance, "ether");
            var formattedBalance = parseFloat(balanceInEther).toFixed(4);
            document.getElementById("response").innerHTML = "Address: " + address + "<br>Balance: " + formattedBalance + " ETH";
        }
    });
}
}

Solution

  • Fixed. It was a combination of incorrect file path AND the "defer" attribute. After correcting the path, the button still didn't work but the .js loaded. Then added "defer" & and extra web3 script call BEFORE the js file call to make sure the web3 connection was established before the Scripts called