Load local javascript file in chrome for testing? - javascript

I am trying to test some JavaScript on my local computer using the Chrome browser but Chrome will not load local resources. Is there an easy work around for this?

If you are trying to just test the functionality of your JavaScript file: create a blank HTML file, add a link to your JS file as you would normally load a JS file from HTML, and open the HTML file in Chrome. Go to the JavaScript console. You'll be able to interact with the functionality of your JS code as usual. You wouldn't need to set up a server for this. If still not clear, here's an example:
<html>
<head>
<script type = "text/javascript" src = "path/to/your/jsfile"></script>
</head>
</html>

You can use a light weight webserver to serve the file.
For example,
1. install Node
2. install the "http-server" (or similar) package
3. Run the http-server package ( "http-server -c-1") from the folder where the script file is located
4. Load the script from chrome console (run the following script on chrome console
var ele = document.createElement("script");
var scriptPath = "http://localhost:8080/{scriptfilename}.js" //verify the script path
ele.setAttribute("src",scriptPath);
document.head.appendChild(ele)
The script is now loaded the browser. You can test it from console.

To load local resources in Chrome when just using your local computer and not using a webserver you need to add the --allow-file-access-from-files flag.
You can have a shortcut to Chrome that allows files access and one that does not.
Create a shortcut for Chrome on the desktop, right click on shortcut, select properties. In the dialog box that opens find the target for the short cut and add the parameter after chrome.exe leaving a space
e.g. C:\PATH TO\chrome.exe --allow-file-access-from-files
This shortcut will allow access to files without affecting any other shortcut to Chrome you have.
When you open Chrome with this shortcut it should allow local resources to be loaded using HTML5 and the filesystem API.

For security reasons, modern browsers won't load resource from locally running HTML files (files using file:// protocol in the address bar).
The easiest way to get a modern browser to load and run JavaScript files in local HTML files is to run a local web server.
If you don't want to go through the trouble of setting up a Node or Apache web server just to test your JavaScript, then I'd suggest you install Visual Studio Code and the Live Server extension.
Visual Studio Code
Visual Studio code is a source code editor for pretty much any programming language under the sun. It has built-in support for JavaScript, HTML, CSS, TypeScript, and almost any kind of language used for Web development.
Install Visual Studio Code
You can get the Visual Studio Code editor for your platform from https://code.visualstudio.com/. It supports Windows, Linux, and Mac. I think it also works on your Surface Pro if that's your thing.
Add the Live Code Extension
After installing VS Code, you can add the Live Code code extension using the Extension panel (Ctrl+Shift+X in Windows) in Visual Studio Code.
Live Server Extension
After adding the extension, you should see a "Go Live" button in the bottom-right corner of the Visual Studio Code IDE (as shown in the above screenshot).
Open in Code
Open the root folder where your HTML and JavaScript files exist in Visual Studio Code and click the "Go Live" button. Optionally, you can right-click the HTML file in the Explorer (Ctrl+Shift+E) and select Open with Live Server from the pop-up menu that appears.
Open with Live Server
This should create a locally running web server and open the file or folder in your web browser. If your file paths are correct, your JavaScript files should also load and run correctly.
Troubleshooting
If for some reason, the page doesn't load in your favorite browser, check that the address and port number are correct. If the Live Server is running, it should display the port number in the bottom-right corner of the Visual Studio IDE. Make sure the address in your browser says http://127.0.0.1:<PORT>/index.html where <PORT> has the same number as shown in the status bar in Visual Studio Code.

Use Chrome browser and with the Web Server for Chrome extension, set a default folder and put your linked html/js files in there, browse to 127.0.0.1:8887 (0r whatever the port is set at) in Chrome and open the developers panel & console. You can then interact with your html/js scripts in the console.

The easiest workaround I have found is to use Firefox. Not only does it work with no extra steps (drag and drop - no muss no fuss), but blackboxing works better than Chrome.

You can do it by a feature of chrome's DevTools: Snippets
Create a new snippets
Copy and paste the file you would like to execute.
Hit CtrlEnter to run the snippet

Windows 8.1 add:
--allow-file-access-from-files
to the end of the target text box after the quotes.
EX: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files
Works like a charm

Running a simple local HTTP server
To test such examples, one needs a local webserver. One of the easiest ways to do this is offered by Python's SimpleHTTPServer (or http.server, depending on the version of Python installed.)
# 1. Install Python 3 & use:
python3 -m http.server
# On windows, instead of "python3" try "python" or "py -3"
# If you installed python version 2 then use:
python -m SimpleHTTPServer
# 2. To serve a specific folder use the --directory flag:
python3 -m http.server --directory /Users/junaid/WebstormProjects
# Note: I use MacOs & I have given the absolute path to my projects folder & I was able to serve my desired folder.

Here's what I did by creating 2 files in the /sandbox directory:
First file: sandbox.js
Second file: index.html
const name = 'Karl'
console.log('This is the name: ' + name)
<html>
<head>
<script type = "text/javascript" src = "file:///Users/karl/Downloads/sandbox/sandbox.js"></script>
</head>
</html>
You can then use Chrome or any browser to inspect and debug/console your code!

setup
You will want to serve the file from a web server. Everything else will be quirky workarounds that might differ greatly from the final result. On the web, files are being served via web servers. You'll want to mimic that locally.
Since you're apparently (at least part-time) being a web developer, do yourself a massive favour and install node, if you haven't already. It comes bundled with the npx binary, which we'll use. Make sure you have a recent LTS version. Bonus points for using n, which will make it easy to stay up to date with your versions, and even switch them as you need.
steps to take
Inside the folder where your file is located: npx http-server -c-1
You can now access the file at http://127.0.0.1:8000/filename
Last thing to do is make the browser load the file. Magesh's answer works well for that, but I would recommend putting that snippet into a "custom js" extension. This one has served me well so far.
With everything in place, simply reload the page to get an updated version
of your local file.

Note: this question comes top on google when you search for "chrome read local css without server". So...
If you really want to serve a local webpage and load its CSS and JS, and you really do not have or don't want to use a http server, then don't load the scripts nor styles; inline them.
Instead of something like this:
<link rel="stylesheet" href="somefile.css">
Define the style like:
<style>/*Here goes all the CSS file content*/</style>
And the same with the JavaScript files. The HTML file will be larger, but it will work.
Basically you just have to copy the file contents and paste it inline.
You really should use a server, but if you are forced to, you can use this method.

If you still need to do this, I ran across the same problem.
Somehow, EDGE renders all the scripts even if they are not via HTTP, HTTPS etc...
Open the html/js file directly from the filesystem with Edge, and it will work.

for my use case, i need to startup a local server, create working an interactive html page, and open it for the user to be able to interact with it. the problem was how to set the local server's web root to my temp working folder where i create these dynamic files. this was a quick and dirty solution.
In the below code, LOCAL_WEB_DIRECTORY is essentially the root. and i then create/copy the files i want to open in the brower to that directory + /templates
import webbrowser
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler
httpd = ThreadingHTTPServer(('', 8001), lambda *_: SimpleHTTPRequestHandler(*_, directory=config.LOCAL_WEB_DIR))
server_thread = threading.Thread(target=httpd.serve_forever, daemon=True)
server_thread.start()
webbrowser.open('http://localhost:8001//templates/' + work_html)
while True: sleep(1)

Not sure why #user3133050 is voted down, that's all you need to do...
Here's the structure you need, based on your script tag's src, assuming you are trying to load moment.js into index.html:
/js/moment.js
/some-other-directory/index.html
The ../ looks "up" at the "some-other-directory" folder level, finds the js folder next to it, and loads the moment.js inside.
It sounds like your index.html is at root level, or nested even deeper.
If you're still struggling, create a test.js file in the same location as index.html, and add a <script src="test.js"></script> and see if that loads. If that fails, check your syntax. Tested in Chrome 46.

The easiest way I found was to copy your file contents into you browser console and hit enter. The disadvantage of this approach is that you can only debug with console.log statements.

Look at where your html file is, the path you provided is relative not absolute. Are you sure it's placed correctly. According to the path you gave in the example above: "src="../js/moment.js" " the JS file is one level higher in hierarchy.
So it should be placed as following:
Parent folder
sub-folder
html file
js (this is a folder)
moment.js
The double dots means the parent folder from current directory, in your case, the current directory is the location of html file.
But to make your life easier using a server will safe you troubles of doing this manually since the server directory is same all time so it's much easier.

Related

Open HTML with CSS in JupyterLab tab with full formatting

I create an HTML document using Sphinx. When I click on the index.html file it opens a browser and looks like this. The look depends on some .CSS and .JS files being executed:
If I open the same file from the JupyterLab file browser, it opens in a tab but looks much worse: .CSS and .JS are not displayed, and images are not displayed. It looks like this:
Is there a way to get JupyterLab to get JupyterLab to execute the .CSS and .JS and pass through any images linked in the text? The JupyterLab is running on a remote server, so I don't have the option of having it create a new browser process on my local machine, because the files are remote.
Using JupyterLab within JupyterHub (old school install with conda, no docker and such)
I've been stuck at this HTML Preview issue for a few weeks.
I have the very same use case as you (Sphinx stuff for a team to work on their docs).
So far, no luck.
It may or may not work (depending on... I'm not sure of...) if I'm using JupyterLab from the browser on the hypervisor hosting JupyterHub itself
It won't work if I'm using JupyterLab from the browser on my client machine.
I tried to mess around with
c.NotebookApp.allow_remote_access = True parameter with no luck
tried to put it in my profile ~/.jupyter/jupyter_notebook_config.py
tried to add it to general config file /path/to/conf/jupyterhub_config.py
=> Not sure of the right way to set this option on JupyterLab's JupyterHub install, nor if it's even a relevant option...
Well, security wise, it's not, that's a given (^^'), but Preview HTML is an important feature for Sphinx users, hope someone can help with this...
I also looked after nginx config, but you get the issue with or without the reverse proxy anyway...

png files failing to load when opening html file directly, but they load when opening from webstorm

I've been working on a game in javascript for my CS course. When I open the document by hitting run in Webstorm, it loads the game correctly, however when I just try opening the html file from Finder, the webpage opens but none of the png files I'm using for the sprites load. I opened Inspect Element in google chrome, and the javascript files loaded correctly but all the png files listed as canceled. This doesnt happen when the game is run from webstorm (when I run it from webstorm, all image files load properly).
When the game is opened directly from an html file (that's when I have the problem), chrome lists the path of the html document as the webaddress, although when opened from webstorm, it lists http://localhost:63342/CS%20Week%2010/CS105_Jessica.Davis_DogGame.html?_ijt=tmrr2fndgac82h07hlvt101gi4
How can I get around this issue so that when opening the html file from Finder it loads everything correctly? All image files are in the same directory as the html file.
Because of browsers security, loading files like this might not work from a url starting with file://
What webstorm is probably is making a local web server so that instead of saying file:// you could say http://. if any website was able to load images from file:// then any webpage you visit would have been able to search for any file on your computer and send it over the internet without your consent so browser often have these settings on. So you'd need a server. If you are working on your computer, you could make a local server just like webstorm and host your own files there. or host it on another service like github pages or codepen.
Now since all images are in the same directory, make sure that every time you call loadImage you use the images name and extension instead of saying /User/user/whatever_other_directory_you_have_it_under/image.png.
Once you did that you can make a local web server for the project. To make a local server, open Terminal (an application under utilities, you could spotlight search for it as well) and type cd, drag your project folder and drop it over terminal, and hit enter. Then type python -m SimpleHTTPServer and wait till it says something like Serving HTTP on 0.0.0.0 port 8000 .... Then taking the 0.0.0.0 and the 8000 you see in the example (yours may or may not be the same) go to your browser and type http://0.0.0.0:8000 (replacing the digits with whatever you got, not this link doesn't work until you do that)
Images should load alright. If you need to stop the server you can go back to terminal and hit control+C.
Note that when presenting your p5 sketch, no one else would be able to see the website on their computers if you make your local server. The local server is secluded to the device that is running it (although if their making their own local server and have your project files it should work just fine).
If you want the website hosted so that you could share a link with anybody in the world you could use codepen or github pages. If you go to codepen.io it should be self-explanatory although you'd have to upload your images to some image hosting site like tumblr or something and add the URL source of those images to codepen or you could put everything into github for even better results!
To use github pages you'd need to make a github account (preferably with your username being whatever you want your page to be named). Make a repository named insert_username_here.github.io. add your files to the repository (make sure to try to keep all sub directories and folder exactly as they are from your project folder). After a minute or two go to http://insert_username_here.github.io to admire your brand new hosted webpage!

Script tag is loading from file:/// instead of http://

I am working with a simple HTML test page. I am opening it with Chrome version 40.0.2214.115. In order to test the code, I am loading a js file from my workplace.
However, the file which I loaded tries to load other js files through script tags, but does not specify http:// . This causes an error in the console, as it defaults to the file:/// protocol and is unable to load file://the/restof/theurl.js
I downloaded the file locally, and loaded it from there, adding http:// to all url's, so it would ACTUALLY use the HTTP protocol.
This worked fine. However, those links load OTHER files that also do not specify http://
Is there any way I can have the browser imply http:// to unspecified script tags, and avoid downloading and editing EVERY file just to test a small HTML page?
I think the easiest way to prevent problems like this is to actually use a very small server (like something provided by node.js or the like or lighttpd).
Obviously your files are not supposed to be delivered via the plain file protocol, so you should just use a http server. That way you can be sure they work as intended and you don't have to fiddle around with your browser on a deeper level.
If you launch chrome with the below flags this should work. Obviously not possible for production environments, but for testing it should be okay.
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files
Consider using a simple local file server. If you are unsure how to, since you are developing in JavaScript, you can easily create a simple file server using Node.js which written in JS using simple middleware like node-static.

Javascript files won't load within Visual Studio ASP.NET 2.0 pro

I have just loaded an existing ASP.NET project into Visual Studio 2010 that is targeted at .NET 2.0. When I attempt to run it, the first page throws an error indicating that the JQuery file was not loaded ($ is undefined). If I paste the contents of the JQuery file within tags on the login page, everything works. This indicates to me that the JQuery file is not being loaded. I have verified that the path is correct.
This problem is not just with the JQuery file but with all JavaScript files, as far as I can tell.
I have copied this entire project directly from someone else's machine. It works perfectly on their machine. The project, on my computer, is at exactly the same path as on their machine.
If I go to IIS on my machine, and right-click on this application under Default Web Site, then choose Manage Application >> Browse, the application is displayed correctly in a browser. (I have verified that the virtual directory in IIS is pointing to my source code folder) So, it appears this issue is specific to the Visual Studio environment.
What could possibly be preventing the Javascript from loading. (the CSS files seem to be loading without error).
Thanks in advance for any debugging advice you can give me.
There can be many things wrong. Have you tried using Fiddler or FireBug and determined if the resources are requested? If they are what is the status that is being returned?
Is Visual Studio using IIS or the built-in Webserver?

run JS app locally in IE

I'm using GWT 2.1 and IE to test the default hello world GWT app.
I compile the default Hello world GWT app and then go to HTML file and
open it with IE.
I get a red warning message "Your web browser must have JavaScript
enabled in order for this application to display correctly."
I had to allow the active content to be running in order to see the
app screen.
things are a bit different if I run the app via \.psf\Home
\myFile.htm which is path to my Parallel's "shared directory" .. I
wouldn't see such warning.
also running in dev mode seems not to raise this warning message.
it seeems IE doesn't like people opening JS-laced web pages from the local drive. Probably a security concern, I would imagine.
there is a detailed article that tackles this issue at http://www.phdcc.com/xpsp2.htm
i tried the method of Mark of the web by adding these two lines at the
begining of myfile.html file :
<!-- saved from url=(0014)about:internet -->
<!-- saved from url=(0016)http://localhost -->
but when i add these two lines and then compile app and open the html file with IE the web app doesn't show even though the warning message is gone!
The reason probably is that (by default) a GWT app is loaded within an iframe, i.e. as another HTML page (*.cache.html), which doesn't contain the MotW.
i Tried using the "xs" or "xsiframe" linker, i.e. add one of these lines to our
*.gwt.xml:
<add-linker name="xs" />
<add-linker name="xsiframe" />
Those linkers use *.cache.js" files, so the MotW in our host page should be enough. but The "xs" linker prevents us from using DevMode .
adding line to xml module file make IE warning message go away and can run app locally with IE.however the line above prevent our app to run in dev mode with IE .
any Idea how we can have the app works both in dev mode and web mode?
i'm not keen on having two xml module files one for dev mode and one
for web mode. but would like to know how to do that if i have to?
also interested in any other method to run javascript apps locally with IE.
thanks
Have you tried enabling Active Content from local files in the advanced tab of the Internet Options?
This worked for me:
SO - Activex content in local web page
The take away is:
<!-- saved from url=(0014)about:internet -->
Must be the first line in the file and the file must have
\r\n (CRLF)
line endings.
you could download and set up xampp and put your files in the htdocs folder. This will give you a local server at your loopback address. This will prevent IE from doing all of its acrobatics concerning locally run js files, as it will think it is accessing a remote website.

Categories

Resources