简体   繁体   中英

How to run a javascript on a browser?

I actually created a javascript to get a plot of statistics of my assembled genome : here is the name of my script: myscript.js

And here is its containt:

<html>
<head>
    <title>assembly stats</title>
  </head>
  <body>
    <link rel="stylesheet" type="text/css" href="css/circle-plot.css">
    <link rel="stylesheet" type="text/css" href="css/square-plot.css">
    <link rel="stylesheet" type="text/css" href="css/table.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
    <script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
    <script type="text/javascript" src="js/circle-plot.js"></script>
    <script type="text/javascript" src="js/square-plot.js"></script>
    <script type="text/javascript" src="js/table.js"></script>
    <div id="assembly_stats">
    <script charset="utf-8" src="d3.js"></script>
    <script>
      d3.json("json/Danaus_plexippus_v3.assembly-stats.json", function(error, json) {
        if (error) return console.warn(error);
        asm = new Assembly (json);
        asm.drawPlot('assembly_stats');
      })
    </script>

  </body>
</html>

and when I try to run by doing: node my_script.js , I get:

/Users/etudiant/Cov_genome/my_script.js:1
(function (exports, require, module, __filename, __dirname) { <html>
                                                              ^

SyntaxError: Unexpected token <
    at new Script (vm.js:74:7)
    at createScript (vm.js:246:10)
    at Object.runInThisContext (vm.js:298:10)
    at Module._compile (internal/modules/cjs/loader.js:646:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
    at Module.load (internal/modules/cjs/loader.js:589:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
    at Function.Module._load (internal/modules/cjs/loader.js:520:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)
    at startup (internal/bootstrap/node.js:228:19)

Does someone know where is the problem? Thank you for your help :)

To run it in a browser, you need to load the HTML document with a browser (and it should have a .html file extension, not a .js file extension.

Node.JS is not a browser. You are getting that error message because you are telling it to interpret HTML as if it were JavaScript.

Use something like Chrome, Firefox, or Edge instead.

If you want to initiate things using Node, then consider a module like Node Webdriver which will let you drive a browser from Node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM