HTML multiple Javascript alias

♀尐吖头ヾ 提交于 2019-12-11 14:00:21

问题


I'm trying to import the image of multiple IP cameras into one page. However, the script the camera serves for the viewer has the same name on every camera so i cannot seem to display both the viewers on one page. Would it be possible to import the scripts with a sort of AS alias so it knows which of the 2 scripts to execute?

In the below pseudo-code I show what i exactly mean (i made up the AS="cam1" and the cam1.~~)

<html>
<head>
        <title>Security Cameras</title>
</head>
<body>
        <script AS="cam1" type='text/javascript' src='http://192.168.1.10:80/jsv/SncViewer.js'></script><script>cam1.SNC.writeViewer({sz:'4',ptz:'1',fps:'15',iPS:'1'})</script>
        <script AS="cam2" type='text/javascript' src='http://192.168.1.20:80/jsv/SncViewer.js'></script><script>cam2.SNC.writeViewer({sz:'4',ptz:'1',fps:'15',iPS:'1'})</script>
</body>
</html>

Thanks in advance


回答1:


I'm guilty for using jQuery here, also I've chosen $(window).load over $(document).on('ready') for this - furthermore it's untested!

$(window).load(function() {
    /**
     *  Array of IPs to load
     *  Empty Cameras = {} object 
     *  for Internal name-spacing.
    **/
    var Ips = ["192.168.1.10:80", "192.168.1.20:80"], 
        Suffix = "/jsv/SncViewer.js",
        Cameras = {};

    /** 
     *  For each IP, get script from
     *  IP Array, Keep the saved variable/SNC
     *  within Camera.camX.
    **/
    for( var x = 0; x < Ips.length; x++ ) {
       $.getScript({
           url: "http://"+ Ips[x] + Suffix +"",
           success: function( data ) {
               //May be data.writeViewer..!
               Cameras['cam'+ x +''] = 
                  SNC.writeViewer({sz:'4',ptz:'1',fps:'15',iPS:'1'});
           } 
       });     
    }
});

//Expected Output.
Cameras = {
    cam1 : //SNC Obj,
    cam2 : //SNC Obj
}



回答2:


I would build a javascript file which will call the scripts using ajax. And load them one by one.



来源:https://stackoverflow.com/questions/19357774/html-multiple-javascript-alias

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!