【问题标题】:How to pass multiple "var url" in same function?如何在同一个函数中传递多个“var url”?
【发布时间】:2014-02-20 18:06:37
【问题描述】:

所以我做了以下,看起来它执行 url1 很好,但 url2 仍然没有被应用。它们单独工作正常,但不能一起工作。

<script type="text/javascript">
<!--
function Start() {
var url1 = "http://stream.xxx.net:7300/event/start";
var url2 = "http://stream.xxx.net:7301/event/start";
window.frames["ifrmInitLiveBroadcast"].location = url1;
window.frames["ifrmInitLiveBroadcast"].location = url2;
alert("Live Broadcast Started") 
}
function Stop() {
var url1 = "http://stream.xxx.net:7300/event/stop";
var url2 = "http://stream.xxx.net:7301/event/stop"; 
window.frames["ifrmInitLiveBroadcast"].location = url1;
window.frames["ifrmInitLiveBroadcast"].location = url2;
alert("Live Broadcast Terminated")
}
//-->
</script>

这是同一脚本中关联的表格代码,也许这里需要更改?

<table style="width: 400" cellspacing="1" class="style7">
<tr>
<td style="height: 50px; width: 200px; text-align: center">
<button onclick="Start()" style="height: 30">Start It</button>                                                                                                       <br>
<iframe name="ifrmInitLiveBroadcast" id="ifrmInitLiveBroadcast" src="" width="0" height="0" scrolling="0" frameborder="0" ></iframe>
</td>
<td style="height: 50px; text-align: center; width: 200px">
<button type="button" onclick="Stop()" style="height: 30px">Stop It</button><br>
<iframe name="ifrmInitLiveBroadcast" id="ifrmInitLiveBroadcast" src="" width="0" height="0" scrolling="0" frameborder="0" ></iframe>
</td>
</tr>
</table>

【问题讨论】:

  • 您的框架似乎都具有相同的名称。

标签: javascript variables var


【解决方案1】:

您可以创建任意数量的唯一命名变量,例如:

 var url1 = 'http:/site1.com'
 var url2 = 'http://site2.com'
 var url3 = 'http://site3.com'

 alert('Visit one of these sites: ' + url1 + ' ' + url2 + ' ' + url3);

【讨论】:

    【解决方案2】:

    是的,但请确保您在 window.frames 中有另一个名为 myUniqueFrameIdentifier 的框架/iframe:

    function Start() {
        var url1 = "http://xxx.yyy.com:7000/event/start";
        var url2 = "http://xxx.yyy.com:7000/event/startAgain";
        window.frames["ifrmInitLiveBroadcast"].location = url1;
        window.frames["myUniqueFrameIdentifier"].location = url2;
        alert("Event Started")
    }
    function Stop() {
        var url1 = "http://xxx.yyy.com:7002/event/stop";
        var url2 = "http://xxx.yyy.com:7000/event/stopAgain";
        window.frames["ifrmInitLiveBroadcast"].location = url1;
        window.frames["myUniqueFrameIdentifier"].location = url2;
        alert("Event Terminated")
    }
    

    另外,在您的代码中,您需要重命名 iframe 之一,id 和/或名称必须是唯一的:

    <table style="width: 400" cellspacing="1" class="style7">
       <tr>
          <td style="height: 50px; width: 200px; text-align: center">
             <button onclick="Start()" style="height: 30">Start It</button>                                                                                                       <br>
             <iframe name="ifrmInitLiveBroadcast" id="ifrmInitLiveBroadcast" src="" width="0" height="0" scrolling="0" frameborder="0" ></iframe>
          </td>
          <td style="height: 50px; text-align: center; width: 200px">
             <button type="button" onclick="Stop()" style="height: 30px">Stop It</button><br>
             <iframe name="myUniqueFrameIdentifier" id="myUniqueFrameIdentifier" src="" width="0" height="0" scrolling="0" frameborder="0" ></iframe>
          </td>
       </tr>
    </table>
    

    【讨论】:

    • 我试过了,但还是缺少什​​么?我编辑了原始问题。谢谢!
    • @user3334034 您的 iframe 具有相同的名称。见上文
    猜你喜欢
    • 2013-09-20
    • 2013-11-21
    • 2012-06-12
    • 2011-04-19
    • 2012-04-14
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多