[英]HTML / JS - Calling a function from index.js onclick
I want to call a function from within my index.js, in my functionality.js but because its linked to the browser I cant use import or require? 我想从我的functional.js中的index.js内调用一个函数,但是因为它链接到浏览器,所以我不能使用import或require?
I have an index.js, index.html and another functionality.js file. 我有一个index.js,index.html和另一个functional.js文件。
The functionality.js file is used by the index.html and within this file I append some html under a specific circumstance and what I am trying to do is onclick call a function but call this function from my index.js file, which has to be there because it needs a socket.id. function.js文件由index.html使用,在此文件中,我会在特定情况下附加一些html,而我试图做的是onclick调用一个函数,但是从我的index.js文件中调用了此函数,在那里,因为它需要一个socket.id。
see here: 看这里:
*functionality.js: * functionality.js:
$('#messages').append($('<li id="messageclient">').append($(`
<div id="message-cont" class="message-cont">
<div class="orderDetailsWrapper">
<div class="detailsHeaderWrapper">
<div class="orderNum"></div>
<div class="customerName"></div>
</div>
<div class="textToCustomer">
<p> Please click on the item you want to return</p>
</div>
<div class="itemBoxWrapper">
<div class="item1Wrapper" onclick="matchingResponse()">
<div class="item1Title"></div>
<div class="item1Price"></div>
</div>
*index.js * index.js
function matchingResponse() {
var options = {
method: 'POST',
uri: 'https://dialogflow.googleapis.com/v2beta1/projects/returnsbot-50668/agent/sessions/1:detectIntent',
headers: {
'Authorization': `Bearer ${oAuthToken}`,
'Content-Type': 'application/json'
},
body: {
"queryInput": {
"event": {
"name": "Matching",
"languageCode": "en"
}
}
},
json: true // Automatically stringifies the body to JSON
};
// initial message triggering.
rp(options)
.then(function (parsedBody) {
console.log('DF response' + JSON.stringify(parsedBody))
console.log('text ^^^^^^^^^^^^^^ ' + JSON.stringify(parsedBody.queryResul))
text = parsedBody.queryResult.fulfillmentText;
response = text;
console.log('matching response +++++++++++' + text);
data = [details.chatuser, response]
io.of('/main').to(socket.id).emit('response', data);
})
.catch(function (err) {
console.log(err);
});
}
Because the socket function is in the index.js file, I need this matchingresponse function inside of it to use the appropriate ID. 因为套接字函数在index.js文件中,所以我需要在其中包含这个matchresponse函数以使用适当的ID。
Trouble is in functionality.js I can't use import / require. 问题是在functional.js中,我无法使用导入/要求。
What can I do? 我能做什么?
如果您未在加载时调用任何函数,则只需向HTML中添加functional.js和index.js文件即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.