简体   繁体   中英

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?

I have an index.js, index.html and another functionality.js file.

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.

see here:

*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

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.

Trouble is in functionality.js I can't use import / require.

What can I do?

如果您未在加载时调用任何函数,则只需向HTML中添加functional.js和index.js文件即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM