简体   繁体   English

未捕获的ReferenceError:未定义Viva

[英]Uncaught ReferenceError: Viva is not defined

New to JS. JS新手。 Not sure why I am getting the below error when I load this in the Chrome. 不知道为什么在Chrome中加载此错误时会出现以下错误。 The vivagraph.js file is in the same directory as the html. vivagraph.js文件与html位于同一目录中。

<!DOCTYPE html>
<html>
<head>
    <title>01. Create Graph. Vivagraph SVG tutorial.</title>
    <script type="text/javascript" src="./vivagraph.js"></script>
    <script type="text/javascript">
        function main () {
            // Step 1. We create a graph object.
            var graph = Viva.Graph.graph();

            // Step 2. We add nodes and edges to the graph:
            graph.addLink(1, 2);

            /* Note: graph.addLink() creates new nodes if they are not yet 
               present in the graph. Thus calling this method is equivalent to:

               graph.addNode(1);
               graph.addNode(2);
               graph.addLink(1, 2);
            */

            // Step 3. Render the graph.
            var renderer = Viva.Graph.View.renderer(graph);
            renderer.run();
        }
    </script>

    <style type="text/css" media="screen">
        html, body, svg { width: 100%; height: 100%;}
    </style>
</head>
<body onload='main()'>

</body>
</html>

If vivagraph.js is in the same directory as html then you can just use src="jsFilename" instead of src="./jsFilename" 如果vivagraph.js与html位于同一目录中,则可以使用src="jsFilename"代替src="./jsFilename"

To remove your error, use jQuery's $(document).ready() : 要消除您的错误,请使用jQuery的$(document).ready()

$(document).ready(function() {
    ...
});

or shorthand one: 或速记之一:

$(function() {
    ...
});

The main() is being executed before the viva script has finished parsing. 在viva脚本完成解析之前,将执行main() You need to tie the main() not to body onload , but rather to document.ready . 您不需要将main()绑定到body onload ,而是绑定到document.ready

A lot of frameworks like jQuery manage the details but you can approximate the same by just putting the call to main in a closure near the bottom of the page: 许多框架(如jQuery)管理这些细节,但是您可以通过将对main的调用放在页面底部附近的闭合中来近似实现这些细节:

</html>
<script type="text/javascript">var foo=(function() {main();})();</script>

Open the debug console in Chrome and check for errors 在Chrome中打开调试控制台,然后检查是否有错误

Alternatives: 备择方案:

  1. can't open file (if on the same dir you can just use "vivagraph.js") 无法打开文件(如果在同一目录下,则只能使用“ vivagraph.js”)
  2. file opening with syntax errors 以语法错误打开文件
  3. file opening ok but different version does not define global object Viva 文件打开正常,但不同版本未定义全局对象Viva

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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