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!
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.
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.
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.
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(/).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With