Skip to content

How to link to different pages in JS? #661

@willwull

Description

@willwull

🤔 Question:

I was wondering how you create links to other HTML pages from JS.
Say I have two files, a.html and b.html and a JS file in a.html with this:

const link = document.createElement("a");
link.setAttribute("href", "./b.html");
link.textContent = "JS link";
document.body.appendChild(link);

Since the built files all get put in the dist folder without any folder structure and hashed names, how do you actually reference other static files? I know you handle src for images by importing the image file in JS as a dependency, but what about HTML files? I've tried b.html, ./b.html, but none work.

🎛 Configuration (.babelrc, package.json, cli command)

{
  "name": "parcel-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "parcel a.html"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "parcel-bundler": "^1.5.0"
  }
}

🤔 Expected Behavior

Clicking on the generated link takes me to b.html.

😯 Current Behavior

Clicking on the link changes the address in the browser to localhost:1234/b.html but doesn't actually load that file.

💁 Possible Solution

Maybe preserve file names so links created in JS will work? Or am I missing something obvious here? 😰

🔦 Context

I'm loading some (external) data through JavaScript and I want to create links to the appropriate pages depending on the data I've loaded in.

💻 Code Sample

None

🌍 Your Environment

Software Version(s)
Parcel 1.5
Node 8.9.3
npm/Yarn 5.6.0
Operating System macOS 10.13.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions