如何执行js函数onload?

时间:2017-08-21 00:56:33

标签: javascript initialization tableau onload

以下是功能齐全且工作正常的代码。当我将其粘贴到文本文件testFile.html然后使用浏览器打开它时,它可以正常工作。

但我想在selectCollege函数

之后立即执行initViz函数

我试过这个

<body onload="initViz();selectCollege('Engineering');"> . . .

但它没有用。如何在selectCollege之后立即执行initViz函数?

<!DOCTYPE html>
<html>

<head>
    <title>Select Marks</title>

    <script type="text/javascript" 
        src="https://public.tableau.com/javascripts/api/tableau-2.min.js"></script>

    <script type="text/javascript">
        var viz, sheet;

        function initViz() {
            var containerDiv = document.getElementById("vizContainer"),
                url = "http://public.tableau.com/views/RegionalSampleWorkbook/College",
                options = {
                    "Academic Year": "",
                    hideTabs: true,
                    onFirstInteractive: function () {
                        sheet = viz.getWorkbook().getActiveSheet();
                    }
                };

            viz = new tableau.Viz(containerDiv, url, options);
        }

        function selectCollege(college_name) {
            sheet.selectMarksAsync("College", college_name, tableau.SelectionUpdateType.REPLACE);
        }

     </script>
</head>

<body onload="initViz();">
    <div id="vizContainer"></div>
    <br />    
    <button onclick="selectCollege('Engineering');">Select a value</button>

</body>

</html>

5 个答案:

答案 0 :(得分:3)

创建新功能

function init(){
  initViz();
  selectCollege('Engineering');
}

然后调用init函数

window.onload = init;

答案 1 :(得分:2)

 function initViz(college_name) {
 //your code

            viz = new tableau.Viz(containerDiv, url, options);
            
            //then
            selectCollege('engineering');
        }

        function selectCollege(college_name) {
            sheet.selectMarksAsync("College", college_name, tableau.SelectionUpdateType.REPLACE);
        }

像这样使用

答案 2 :(得分:2)

这对我有用

&#13;
&#13;
function bar() {
  alert("bar");
}
function foo() {
  alert("foo");
}
&#13;
<!DOCTYPE html>
<html>
<body onload="bar();foo();">

</body>
</html>
&#13;
&#13;
&#13;

答案 3 :(得分:2)

selectCollege() sheet中,您试图在tableau.Viz options对象的回调函数中定义onFirstInteractive()之前访问sheetoptions = { ... onFirstInteractive: function () { sheet = viz.getWorkbook().getActiveSheet(); selectCollege('Engineering'); } }; 。为了解决这个问题,您可以在该函数中定义onFirstInteractive()后调用该函数:

tableau.Viz

根据this forum <div class="navbar navbar-fixed-top navbar-inverse"> <div class="navbar-inner"> <div class="container"> <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </a> <div class="nav-collapse collapse"> <ul class="nav"> <li><a href="dashboard.php"><i class="icon-home icon-large"></i>&nbsp;Home</a></li> <li><a href="attendance.php"><i class="icon-home icon-large"></i>&nbsp;Attendance Info</a></li> <li><a href="users.php"><i class="icon-user icon-large"></i>&nbsp;Users</a></li> <?php include('dropdown.php'); ?> <li><a href="books.php"><i class="icon-book icon-large"></i>&nbsp;Books</a></li> <li><a href="member.php"><i class="icon-group icon-large"></i>&nbsp;Member</a></li> <li ><a href="archive.php"><i class="icon-list-alt icon-large"></i>&nbsp;Archive</a></li> <li><a href="utility.php"><i class="icon-cog icon-large"></i>&nbsp;Utilities</a></li> <li><a href="logout.php"><i class="icon-signout icon-large"></i>&nbsp;Logout</a></li> </ul> <div class="pull-right"> <div class="admin">Welcome: <?php if(isset($_SESSION['id'])){ // dont know what put in here } ?></div> </div> </div> </div> </div> </div> <?php include('search_form.php'); ?> 首次呈现<?php if(!isset($_SESSION)){ session_start(); } ?> <?php if (!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) { ?> <script>window.location='index.php';</script> <?php exit(); } $session_id=$_SESSION['id']; ?> 的实例时会被调用一次。

答案 4 :(得分:0)

...
<body>
... All other tags..
<script>
//just before closing body
function(){

}()
</script>
</body>
....

在脚本标记中关闭body之前调用您的函数。

按顺序解释HTML树。这就是我如何为SPA添加加载消息。