-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to link to different pages in JS? #661
Comments
Heyyo, I'd like to help out with this one. However, I'm new to this project and would appreciate a little bit of direction 🙂 |
@Powell-v2 Hop onto the Slack and message us! We’re always happy to help :) |
@Powell-v2 Any progress on this? |
I asked a question on Slack's |
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 |
@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:
I keep getting the error: "Cannot find module". Is there something different I should be doing with my import statement? |
@willwull, were you able to get this working? |
@ericperrotta I gave up on this 😅 |
Currently stuck on this too. With an import indexPage from './index.html'
console.log('yay', indexPage) I keep getting
|
Sample code: https://github.com/felixrabe/e-2018-051-parcel-1.7.1-include-html-from-js Run with |
My goal is (in Electron main process) to figure out the correct HTML path to supply to |
Hey @davidnagli, Currently, this does not work import loginPage from 'login.html'
/* stuff... */
<a href=loginPage>Login</a> Seems like #1119 might be a workaround #1239 is interesting as well. |
Currently, this import site from "./pages/site.html";
console.log(site); will print out the contents of that html file. |
Followup: #1732 actually made html imports return the content and not the url. |
This needs some design work (e.g. #2306) on how to specify if the url or actual content should be imported |
I realised this was possible with today's plugins by treating a magic file extension as a signal to import the asset as an URL, and put that together, plus instructions. I think there's still value in having this baked into Parcel because this is definitely a hack, but it's got the virtue of being a hack that works. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. |
🤔 Question:
I was wondering how you create links to other HTML pages from JS.
Say I have two files,
a.html
andb.html
and a JS file ina.html
with this: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 handlesrc
for images by importing the image file in JS as a dependency, but what about HTML files? I've triedb.html
,./b.html
, but none work.🎛 Configuration (.babelrc, package.json, cli command)
🤔 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
The text was updated successfully, but these errors were encountered: