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

前端 未结 23 1607
[愿得一人]
[愿得一人] 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 17:50

    Open custom Chrome with URL from prompt

    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "Open Chrome",
          "type": "process",
          "windows": {
            "command": "${config:chrome.executable}"
          },
          "args": ["--user-data-dir=${config:chrome.profileDir}", "${input:url}"],
          "problemMatcher": []
        }
      ],
      "inputs": [
        {
          "id": "url",
          "description": "Which URL?",
          "default": "http://localhost:8080",
          "type": "promptString"
        }
      ]
    }
    

    Open custom Chrome with active file

    {
      "label": "Open active file in Chrome",
      "type": "process",
      "command": "chrome.exe",
      "windows": {
        "command": "${config:chrome.executable}"
      },
      "args": ["--user-data-dir=${config:chrome.profileDir}", "${file}"],
      "problemMatcher": []
    },
    

    Notes

    • if necessary, replace windows property by other OS
    • replace ${config:chrome.executable} with your custom chrome location, e.g. "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
    • replace ${config:chrome.profileDir} with your custome chrome profile directory, e.g. "C:/My/Data/chrome/profile" or leave it out
    • You can keep the variables like above if you want. To do so, add following entries in settings.json - user or workspace - , adjust paths to your needs:
    "chrome.executable": "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe",
    "chrome.profileDir": "C:/My/Data/chrome/profile"
    
    • You could re-use these variables e.g. in launch.json for debugging purposes: "runtimeExecutable": "${config:chrome.executable}"
    0 讨论(0)
  • 2020-11-28 17:52

    my runner script looks like :

    {
        "version": "0.1.0",
    
        "command": "explorer",
    
        "windows": {
            "command": "explorer.exe"
        },
    
        "args": ["{$file}"]
    }
    

    and it's just open my explorer when I press ctrl shift b in my index.html file

    0 讨论(0)
  • 2020-11-28 17:54

    Recently came across this feature in one of the visual studio code tutorial in www.lynda.com

    Press Ctrl + K followed by M, it will open the "Select Language Mode" ( or click on the right hand bottom corner that says HTML before that smiley ), type markdown and press enter

    Now Press Ctrl + K followed by V, it will open your html in a near by tab.

    Tadaaa !!!

    Now emmet commands were not working in this mode in my html file, so I went back to the original state ( note - html tag tellisense were working perfectly )

    To go to original state - Press Ctrl + K followed by M, select auto-detect. emmet commands started to work. If you are happy with html only viewer, then there is no need for you to come back to the original state.

    Wonder why vscode is not having html viewer option by default, when it is able to dispaly the html file in the markdown mode.

    Anyway it is cool. Happy vscoding :)

    0 讨论(0)
  • 2020-11-28 17:56

    For Mac, set your tasks.json (in the .vscode folder) file contents to the following and use SHFT+COMMAND+B to open.

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Chrome Preview",
                "type": "shell",
                "command": "open -a /Applications/Google\\ Chrome.app test.html",
                "problemMatcher": [],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }
    
    0 讨论(0)
  • 2020-11-28 17:59

    One click solution simply install open-in-browser Extensions from the Visual Studio marketplace.

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

    I am just re-posting the steps I used from msdn blog. It may help the community.

    This will help you to setup a local web server known as lite-server with VS Code, and also guides you to host your static html files in localhost and debug your Javascript code.

    1. Install Node.js

    If not already installed, get it here

    It comes with npm (the package manager for acquiring and managing your development libraries)

    2. Create a new folder for your project

    Somewhere in your drive, create a new folder for your web app.

    3. Add a package.json file to the project folder

    Then copy/paste the following text:

    {
       "name": "Demo",
       "version": "1.0.0",
       "description": "demo project.",
       "scripts": {
         "lite": "lite-server --port 10001",
         "start": "npm run lite"
       },
       "author": "",
       "license": "ISC",
       "devDependencies": {
         "lite-server": "^2.5.4"
       }
    }
    

    4. Install the web server

    In a terminal window (command prompt in Windows) opened on your project folder, run this command:

    npm install
    

    This will install lite-server (defined in package.json), a static server that loads index.html in your default browser and auto refreshes it when application files change.

    5. Start the local web server!

    (Assuming you have an index.html file in your project folder).

    In the same terminal window (command prompt in Windows) run this command:

    npm start
    

    Wait a second and index.html is loaded and displayed in your default browser served by your local web server!

    lite-server is watching your files and refreshes the page as soon as you make changes to any html, js or css files.

    And if you have VS Code configured to auto save (menu File / Auto Save), you see changes in the browser as you type!

    Notes:

    • Do not close the command line prompt until you’re done coding in your app for the day
    • It opens on http://localhost:10001 but you can change the port by editing the package.json file.

    That’s it. Now before any coding session just type npm start and you are good to go!

    Originally posted here in msdn blog. Credits goes to Author : @Laurent Duveau

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