1

I really can't figure out how to do it. I need to call somefunc() from file.js file on page load.

My file.js contains:

function somefunc() {
    pc.somefunc(gotLocalDescription,

    function(error) {
        console.log(error)
    }, {
        'mandatory': {
            'OfferToReceiveAudio': true,
            'OfferToReceiveVideo': true
        }
    });
}

// Socket.io    
var socket = io.connect('', {
    port: 1234
});

function sendCall(call) {
    socket.emit('call', call);
}

socket.on('call', function(call) {
    if (call.type === 'offer') {
        pc.setRemoteDescription(new SessionDescription(call));
        createAnswer();
    } else if (call.type === 'answer') {
        console.log('10--if call type is answer');
        pc.setRemoteDescription(new SessionDescription(call));
    } else if (call.type === 'candidate') {
        var candidate = new IceCandidate({
            sdpMLineIndex: call.label,
            candidate: call.candidate
        });
        pc.addIceCandidate(candidate);
    }
});
Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
tagaism
  • 624
  • 2
  • 7
  • 26

4 Answers4

0

consider using

<a href="#" onClick="function_name()">Trigger</a>

instead

jsonb.uy
  • 104
  • 1
  • 5
  • There won't be any issue on calling js function using achor href attribute, if you calling it using javascript:somefunc(). – Parkash Kumar Dec 21 '15 at 10:43
  • There may not be any issue but this http://stackoverflow.com/questions/1070760/javascript-function-in-href-vs-onclick have a better answer – brk Dec 21 '15 at 11:10
0

You can simple call the function which is in another file. Have created a plunker.Also note it has a seperate file file.js. If you using name spacing please take care of that.

<a href="javascript:_testMe()">Click Me</a>

WORKING COPY

brk
  • 48,835
  • 10
  • 56
  • 78
  • Thanks @user2181397, actually I want my function to run, when page is loaded. – tagaism Dec 21 '15 at 10:57
  • Not sure about exact requirement,you attached the function with href attribute & now want it to run on page load !?. If are looking to run it on page load use window.load or if you have jquery use $(document).ready(function(){..} – brk Dec 21 '15 at 11:08
  • I did like you've offered, and it caused an error in js console: Uncaught TypeError: Cannot read property 'createOffer' of undefined – tagaism Dec 21 '15 at 11:13
0

Using window.onload (pure javascript), you can call your somefunc() of file.js on page load, as following:

function somefunc() {
  alert('somefunc() of file.js called!');

  /*
  * Your logic goes here.
  */
}

window.onload = somefunc();

DEMO

While, if you want to use jQuery, then include jquery source first and then your custom script file containing your method and DOM ready call, as following:

function somefunc() {
  alert('somefunc() of file.js called!');

  /*
  * Your logic goes here.
  */
}

jQuery(document).ready(function(){
    somefunc();
});

// OR 

jQuery(function({
    somefunc();
});

First make sure you have included your file.js to head of your html and the location to file is correct.

Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
0

You can use this:

<a href="#" onClick="javascript:function_name()"> click </a>

But first you must include file.js in you html

<script type="text/javascript" src="file.js">
Irrech
  • 1,277
  • 2
  • 13
  • 18