How to view an HTML file in the browser with Visual Studio Code

前端 未结 23 1609
[愿得一人]
[愿得一人] 2020-11-28 17:43

How can I view my HTML code in a browser with the new Microsoft Visual Studio Code?

With Notepad++ you have the option to Run in a browser. How can I do the same thi

相关标签:
23条回答
  • 2020-11-28 18:05

    Ctrl + F1 will open the default browser. alternatively you can hit Ctrl + shift + P to open command window and select "View in Browser". The html code must be saved in a file (unsaved code on the editor - without extension, doesn't work)

    0 讨论(0)
  • 2020-11-28 18:06

    In linux, you can use the xdg-open command to open the file with the default browser:

    {
        "version": "0.1.0",
        "linux": {
            "command": "xdg-open"
        },
        "isShellCommand": true,
        "showOutput": "never",
        "args": ["${file}"]
    }
    
    0 讨论(0)
  • 2020-11-28 18:08

    @InvisibleDev - to get this working on a mac trying using this:

    {
        "version": "0.1.0",
        "command": "Chrome",
        "osx": {
            "command": "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
        },
        "args": [
            "${file}"
        ]
    }
    

    If you have chrome already open, it will launch your html file in a new tab.

    0 讨论(0)
  • 2020-11-28 18:10

    If you would like to have live reload you can use gulp-webserver, which will watch for your file changes and reload page, this way you don't have to press F5 every time on your page:

    Here is how to do it:

    • Open command prompt (cmd) and type

      npm install --save-dev gulp-webserver

    • Enter Ctrl+Shift+P in VS Code and type Configure Task Runner. Select it and press enter. It will open tasks.json file for you. Remove everything from it end enter just following code

    tasks.json

    {
        "version": "0.1.0",
        "command": "gulp",
        "isShellCommand": true,
        "args": [
            "--no-color"
        ],
        "tasks": [
            {
                "taskName": "webserver",
                "isBuildCommand": true,
                "showOutput": "always"
            }
        ]
    }
    
    • In the root directory of your project add gulpfile.js and enter following code:

    gulpfile.js

    var gulp = require('gulp'),
        webserver = require('gulp-webserver');
    
    gulp.task('webserver', function () {
        gulp.src('app')
            .pipe(webserver({
                livereload: true,
                open: true
            }));
    });
    
    • Now in VS Code enter Ctrl+Shift+P and type "Run Task" when you enter it you will see your task "webserver" selected and press enter.

    Your webserver now will open your page in your default browser. Now any changes that you will do to your HTML or CSS pages will be automatically reloaded.

    Here is an information on how to configure 'gulp-webserver' for instance port, and what page to load, ...

    You can also run your task just by entering Ctrl+P and type task webserver

    0 讨论(0)
  • 2020-11-28 18:10

    Openning files in Opera browser (on Windows 64 bits). Just add this lines:

    {
    "version": "0.1.0",
    "command": "opera",
    "windows": {
        "command": "///Program Files (x86)/Opera/launcher.exe"
    },
    "args": ["${file}"] }
    

    Pay attention to the path format on "command": line. Don't use the "C:\path_to_exe\runme.exe" format.

    To run this task, open the html file you want to view, press F1, type task opera and press enter

    0 讨论(0)
提交回复
热议问题