Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use absolute paths in Electron?

Simple question here that I can't seem to find a straight answer for.

Is there any way to use absolute paths for dependencies (e.g. <script src="/my-script.js"></script>) with electron and have it work?

Currently it just does mainWindow.loadURL('file://' + __dirname + '/index.html');

Which loads index.html just fine, but here's the thing, index.html loads

Which fails because it looks in the root of the entire hard drive

This would make my life quite a bit easier, as otherwise i'd have to refactor a bunch of template URLs, and doing some would forever break my app if I ever wanted to add pages in a subdirectory on the website (e.g. http://website.com/m/).

Any suggestions? Thanks!

like image 592
Colton Voege Avatar asked Feb 23 '16 03:02

Colton Voege


People also ask

When would we want to use an absolute path?

There are two basic choices when it comes to selection of URLs, absolute URL and relative URL. An absolute URL points to a very specific location on the Web and contains all the necessary information to locate a resource. Absolute URLs must be used to link to other websites that are not located on the same domain.

What is an example of an absolute path?

An absolute path always contains the root element and the complete directory list required to locate the file. For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string.

Is absolute path better than relative path?

To find the full absolute path of the current directory use the pwd command. It is a best practice to use relative file paths (if possible). When using relative file paths, your web pages will not be bound to your current base URL.

What is absolute path in Shell?

A path is a unique location to a file or a folder in a file system of an OS. A path to a file is a combination of / and alpha-numeric characters. Absolute Path-name. An absolute path is defined as the specifying the location of a file or directory from the root directory(/).


1 Answers

You can override built-in URI to file-path resolution using protocol.interceptFileProcotol(), your handler can then map /my-script.js to whatever path you want.

like image 111
Vadim Macagon Avatar answered Sep 28 '22 09:09

Vadim Macagon