Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在页面加载时从.js文件调用javascript函数?_Javascript_Html - Fatal编程技术网

如何在页面加载时从.js文件调用javascript函数?

如何在页面加载时从.js文件调用javascript函数?,javascript,html,Javascript,Html,我真的不知道怎么做。我需要在页面加载时从file.js文件调用somefunc() Myfile.js包含: function somefunc() { pc.somefunc(gotLocalDescription, function(error) { console.log(error) }, { 'mandatory': { 'OfferToReceiveAudio': true, '

我真的不知道怎么做。我需要在页面加载时从
file.js
文件调用
somefunc()

My
file.js
包含:

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);
    }
});
考虑使用

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

相反

考虑使用

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


相反,您可以简单地调用另一个文件中的函数。 已经创建了一个plunker。还要注意它有一个单独的文件file.js。如果您使用名称间距,请注意这一点

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


您可以简单地调用另一个文件中的函数。 已经创建了一个plunker。还要注意它有一个单独的文件file.js。如果您使用名称间距,请注意这一点

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


使用
window.onload
(纯javascript),您可以在页面加载时调用
file.js
somefunc()
,如下所示:

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

  /*
  * Your logic goes here.
  */
}

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

  /*
  * Your logic goes here.
  */
}

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

// OR 

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

同时,如果您想使用
jQuery
,则首先包括jQuery源代码,然后包括包含方法和DOM
ready
调用的自定义脚本文件,如下所示:

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

  /*
  * Your logic goes here.
  */
}

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

  /*
  * Your logic goes here.
  */
}

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

// OR 

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

首先确保已将
文件.js
包含在html的开头,并且文件的位置正确。

使用
窗口.onload
(纯javascript),可以在页面加载时调用
文件.js的
somefunc()
,如下所示:

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

  /*
  * Your logic goes here.
  */
}

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

  /*
  * Your logic goes here.
  */
}

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

// OR 

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

同时,如果您想使用
jQuery
,则首先包括jQuery源代码,然后包括包含方法和DOM
ready
调用的自定义脚本文件,如下所示:

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

  /*
  * Your logic goes here.
  */
}

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

  /*
  * Your logic goes here.
  */
}

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

// OR 

jQuery(function({
    somefunc();
});
首先确保已将
文件.js
包含在html的开头,并且文件的位置正确无误。

您可以使用以下方法:

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

但首先必须在html中包含file.js

<script type="text/javascript" src="file.js">

您可以使用以下功能:

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

但首先必须在html中包含file.js

<script type="text/javascript" src="file.js">



函数是javascript中的关键字/保留字。您不能将其用作函数名,请将其更改为其他名称,然后重试。
function
name永远不能是
function
。Post
file.js
content,我认为其中存在严重错误。
javascript:function()
也有错误。为什么函数调用也在href属性中?您通常会将其视为一次点击事件。谢谢,Parkash Kumar。我只是举个例子写的。我的函数实际上是不同的名称。无论如何,我为您编辑了它。函数是javascript中的关键字/保留字。您不能将其用作函数名,请将其更改为其他名称,然后重试。
function
name永远不能是
function
。Post
file.js
content,我认为其中存在严重错误。
javascript:function()
也有错误。为什么函数调用也在href属性中?您通常会将其视为一次点击事件。谢谢,Parkash Kumar。我只是举个例子写的。我的函数实际上是不同的名称。无论如何,我为您编辑了它。如果您使用javascript:somefunc()调用js函数,则使用achor href属性调用js函数不会有任何问题。可能不会有任何问题,但这有一个更好的答案如果您使用javascript:somefunc()调用js函数,则使用achor href属性调用js函数不会有任何问题。可能没有任何问题,但这有一个更好的答案谢谢@user2181397,实际上我希望在加载页面时运行我的函数。不确定确切的要求,您附加了href属性的函数&现在希望它在加载页面时运行!?。如果您希望在页面加载上运行它,请使用window.load,或者如果您有jquery,请使用$(document.ready)(function(){..}我确实喜欢你提供的,但它在js控制台中导致了一个错误:Uncaught TypeError:无法读取UndefinedHanks@user2181397的属性'createOffer',实际上我希望我的函数在页面加载时运行。不确定确切的要求,你附加了href属性的函数&现在希望它在页面加载时运行!?。如果你想运行它在页面加载时使用window.load,或者如果您有jquery,则使用$(document.ready)(function(){..}我确实喜欢您提供的,它在js控制台中导致了一个错误:uncaughtTypeError:无法读取的属性'createOffer'undefined@tagaism,我想这是你想要的。不是吗?谢谢你的帮助。但我想我没有传达问题的意思。不管怎样,如果我找到了,我会寻找解决方案并让你知道。哦,走吧嘘,你仍然有问题。@Tagaim,这是我认为你想要的。不是吗?谢谢你的帮助。但我想我没有传达问题的意思。无论如何,我会寻找解决方案,如果我找到了,会告诉你的。哦,天哪,你仍然有问题。