question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to link to different pages in JS?

See original GitHub issue

🤔 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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ericperrottacommented, Apr 3, 2018

@davidnagli: I am new parcel.js and frontend development in general. I am trying to do as you suggested and trying to import the html. I keep getting an error with my import statement. Since I am new to parcel and CommonJS, I am probably not structuring the import correctly. In my case I am trying to import an file that is 2 directories up from the file where the import statement is located. My import statement looks something like this:

import longForm from '../../long_form.html'

I keep getting the error: “Cannot find module”. Is there something different I should be doing with my import statement?

1reaction
davidnaglicommented, Mar 16, 2018

Upon further review and discussion, I think that this feature isn’t very useful, and would be nearly impossible to implement.

You can accomplish the exact same thing by importing the html, which will return a URI. For example:

import loginPage from 'login.html'

const link = document.createElement("a");
link.setAttribute("href", loginPage);
link.textContent = "Login Page";
document.body.appendChild(link);

It’s just a string with the URI, so you can use it in any way you want:

import loginPage from 'login.html'

/* stuff... */

<a href=loginPage>Login</a>

Importing HTML files (at least in Parcel 1.x) will always resolve that file and return a URI pointing to it, not the actual file contents.

Note: This also works in the same exact way with CommonJS require

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to go from one page to another page using javascript?
So if your code was already at http://example.com , then simply window.location.href = 'new_url'; would work. This is particularly useful if ...
Read more >
How To Redirect to Another Webpage - W3Schools
Redirect a Webpage. There are a couple of ways to redirect to another webpage with JavaScript. The most popular ones are location.href and...
Read more >
How to create a link in JavaScript ? - GeeksforGeeks
Create an anchor <a> element. · Create a text node with some text which will display as a link. · Append the text...
Read more >
Link Component - Navigate Between Pages | Learn Next.js
When linking between pages on websites, you use the <a> HTML tag. ... First, open pages/index.js , and import the Link component from...
Read more >
Linking Between Pages | Gatsby
This guide covers how to link between pages in a Gatsby site. ... which the referrer page can be replaced dynamically in JavaScript...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found