简体   繁体   English

尝试从另一个JavaScript更改JavaScript变量

[英]Trying to change javascript variable from another javascript

I'm trying to use an external tool called yEd within my website. 我正在尝试在网站内使用名为yEd的外部工具。

I created a select with some graphs as values and when the user clicks on an option the graph will show up. 我创建了一个带有一些图形作为值的选择,当用户单击一个选项时,图形将显示出来。

<div class="col-sm-3">    
    <select multiple class="form-control" id="graph" name="graph" >
        <option value="yed/files/hardware/1.graphml">#1</option> <!-- GET value of this option-->
        <option value="files/hardware/2.graphml">#2</option>
        <option value="files/hardware/3.graphml">#3</option>
    </select>
</div>

<input type="button" id="change" value="change">

<div id="graph">
    <script type="text/javascript" language="javascript">
            <!--

            if (!RunPlayer(
                "width", "100%",
                "height", "100%",
                "graphUrl", "yed/files/hardware/1.graphml", //PUT here when option is clicked and reload the graph
                "overview", "true",
                "toolbar", "true",
                "tooltips", "false",
                "movable", "true",
                "links", "true",
                "linksInNewWindow", "true",
                "viewport", "full"
                )) {
              if (!InstallFlashUpdate("width", "100%", "height", "100%")) {
                document.write('This content requires the Adobe Flash Player Version 9.0.38 or higher. '
                    + '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>');
              }
            }



            //-->
    </script>
</div>

I tried with javascript like so: 我尝试使用javascript这样:

<script type="text/javascript">
var a = "yed/files/hardware/1.graphml";
function showDiv(toggle){
    a = "yed/files/hardware/2.graphml";

}
</script>

But it failed miserable. 但是失败了惨不忍睹。

Any advices? 有什么建议吗? Thank you 谢谢

--- LATER EDIT ---后来编辑

I tried this but it reloads the page, the viwer starts but the graph doesn't load. 我试过了,但是它重新加载了页面,启动了viwer,但是没有加载图形。

<script type="text/javascript" language="javascript">
        <!--

            var graph = "yed/files/hardware/1.graphml";

            RunPlayer(
            "graphUrl", graph,
            "overview", "true",
            "toolbar", "true",
            "tooltips", "false",
            "movable", "true",
            "links", "true",
            "linksInNewWindow", "true",
            "viewport", "full"
            );

             $("#graph").change(function () {
                    var graph = $("#graph").val();
                    RunPlayer(
                        "graphUrl", graph,
                        "overview", "true",
                        "toolbar", "true",
                        "tooltips", "false",
                        "movable", "true",
                        "links", "true",
                        "linksInNewWindow", "true",
                        "viewport", "full"
                        );
                });



        //-->
</script>

The variable is set correct. 该变量设置正确。

A couple things: 几件事:

For readability, I would do this part first instead of nesting it inside another conditional: 为了提高可读性,我将首先执行此部分,而不是将其嵌套在另一个条件中:

if (!InstallFlashUpdate({width: "100%", height: "100%")) {
    document.write(
        'This content requires the Adobe Flash Player Version 9.0.38' +
        'or higher. <a href="http://www.adobe.com/go/getflash/">'+
        'Get Flash</a>'
    );
}
else{

It looks like you mean to pass in a plain object to RunPlayer. 看起来您的意思是将普通对象传递给RunPlayer。 I would define it first and then pass it in: 我先定义它,然后再传递给它:

var settings = {
            width: "100%",
            height: "100%",
            graphUrl: "yed/files/hardware/1.graphml",
            overview: true,
            toolbar: true,
            tooltips: false,
            movable: true,
            links: true,
            linksInNewWindow: true,
            viewport: "full"
};

Now run it as is when the user hasn't selected anything: 现在,当用户未选择任何内容时,按原样运行它:

RunPlayer(settings);

However, we still don't have it changing when the user changes options. 但是,当用户更改选项时,我们仍然没有更改它。 We do that by adding a listener that runs an anonymous function that: 为此,我们添加了一个运行匿名函数的侦听器,该函数可以:

Step 1. Changes graphUrl in our settings to the value in our drop down. 步骤1.将设置中的graphUrl更改为下拉菜单中的值。 Step 2. Calls RunPlayer again with the changed settings. 步骤2.使用更改后的设置再次调用RunPlayer。

var urlChanger = document.getElementById('change');

urlChanger.addEventListener('change', function(event){
    settings.graphUrl = event.target.value; //step 1
    RunPlayer(settings); //step 2
});

}//close your else } //关闭其他

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

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