【问题标题】:How to open url in new tab from JavaScript script如何从 JavaScript 脚本在新选项卡中打开 url
【发布时间】:2021-02-12 17:07:22
【问题描述】:

我正在使用 peekabot 开发一个聊天机器人(下面提供了文档和官方示例的链接),并且希望能够在新选项卡中打开一个 URL(因为您可以选择使用纯 html)当用户点击一个选项。与此相关的是生成的 URL 按钮样式错误(鼠标悬停时显示为空白,没有标签文本)。

我在下面包含了整个脚本的上下文,但我指的是:

6: {
            text: 'You should check it out on test preview',
            options: [{
                text: "PREVIEW THIS LINK",
                url: "www.google.com"
            }]
        },

以上是选项 6,点击它后,用户应该转到 URL(例如 google.com),但在新标签中打开,并且也不是空白(由于某种原因,悬停时按钮链接为空白)

我尝试过:url: "www.google.com" target="_blank(见下文),但这会破坏整个 javascript 代码。

6: {
                text: 'You should check it out on test preview',
                options: [{
                    text: "PREVIEW THIS LINK",
                    url: "www.google.com" target="_blank
                }]
            },

我也试过了:

url: "www.google.com target="_blank" 

url: "www.google.com target=_blank"

都不行。

如需回答:

  1. 在新标签页中打开 URL 的解决方案
  2. 更正,使按钮链接(用于 URL)在悬停时不为空白。 (当您将鼠标从按钮链接上移开时,会出现文字)

这是官方网站 - https://peekobot.github.io/peekobot/ 但即使在他们的示例中也令人恼火 - GitHub 的 URL 在同一个选项卡中打开。

下面的整个脚本的上下文:

</script>
<script type="text/javascript" src="<?php echo base_url(''); ?>js/bot.js>v=1"></script>

<script>
const chat = {
    1: {
        text: 'Some Text here',
        options: [{
            text: 'More Text here',
            next: 101
        },
     {
            text: '??',
            next: 201
        }
        ,
         {
            text: '?????',
            next: 301
        }   
    ]
    },
    101: {
        text: 'Some information here,
        options: [{
            text: 'That is great, thanks!',
            next: 102
        },
        {
            text: 'I would like some more info please.',
            next: 103
        }]
    },
    102: {
        text: 'Thanks and Goodbye',
        url: siteurl + "index.php/student/test",
        options: [{
            text: 'Bye and thank you.',
            next: 3
        },
        {
            text: 'I would like some more info please.',
            next: 104
        }]
    },
    103: {
        text: 'Info here',
        options: [{
            text: 'That is great, thanks!',
            next: 103
        },
        {
            text: 'I would like some more info please.',
            next: 104
        }]
    },
    5: {
        text: 'Aah, you\'re missing out!',
        next: 6
    },
    6: {
        text: 'You should check it out on test preview',
        options: [{
            text: "Go to PRIVIEW",
            url: "www.google.com"
        }]
    },
    
};


const bot = function () {

    const peekobot = document.getElementById('peekobot');
    const container = document.getElementById('peekobot-container');
    const inner = document.getElementById('peekobot-inner');
    let restartButton = null;

    const sleep = function (ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    };

    const scrollContainer = function () {
        inner.scrollTop = inner.scrollHeight;
    };

    const insertNewChatItem = function (elem) {
        //container.insertBefore(elem, peekobot);
        peekobot.appendChild(elem);
        scrollContainer();
        //debugger;
        elem.classList.add('activated');
    };

    const printResponse = async function (step) {
        const response = document.createElement('div');
        response.classList.add('chat-response');
        response.innerHTML = step.text;
        insertNewChatItem(response);

        await sleep(1500);

        if (step.options) {
            const choices = document.createElement('div');
            choices.classList.add('choices');
            step.options.forEach(function (option) {
                const button = document.createElement(option.url ? 'a' : 'button');
                button.classList.add('choice');
                button.innerHTML = option.text;
                if (option.url) {
                    button.href = option.url;
                } else {
                    button.dataset.next = option.next;
                }
                choices.appendChild(button);
            });
            insertNewChatItem(choices);
        } else if (step.next) {
            printResponse(chat[step.next]);
        }
    };

    const printChoice = function (choice) {
        const choiceElem = document.createElement('div');
        choiceElem.classList.add('chat-ask');
        choiceElem.innerHTML = choice.innerHTML;
        insertNewChatItem(choiceElem);
    };

    const disableAllChoices = function () {
        const choices = document.querySelectorAll('.choice');
        choices.forEach(function (choice) {
            choice.disabled = 'disabled';
        });
        return;
    };

    const handleChoice = async function (e) {

        if (!e.target.classList.contains('choice') || 'A' === e.target.tagName) {
            // Target isn't a button, but could be a child of a button.
            var button = e.target.closest('#peekobot-container .choice');

            if (button !== null) {
                button.click();
            }

            return;
        }

        e.preventDefault();
        const choice = e.target;

        disableAllChoices();

        printChoice(choice);
        scrollContainer();

        await sleep(1500);

        if (choice.dataset.next) {
            printResponse(chat[choice.dataset.next]);
        }
        // Need to disable buttons here to prevent multiple choices
    };

    const handleRestart = function () {
        startConversation();
    }

    const startConversation = function () {
        printResponse(chat[1]);
    }

    const init = function () {
        container.addEventListener('click', handleChoice);

        restartButton = document.createElement('button');
        restartButton.innerText = "Restart";
        restartButton.classList.add('restart');
        restartButton.addEventListener('click', handleRestart);

        container.appendChild(restartButton);

        startConversation();
    };

    init();
}

bot();
</script>

更新:

根据以下建议,我已经尝试过:

if (step.options) {
            const choices = document.createElement('div');
            choices.classList.add('choices');
            step.options.forEach(function (option) {
                const button = document.createElement(option.url ? 'a' : 'button');
                button.classList.add('choice');
                button.innerHTML = option.text;
                if (option.url) {
                    button.href = option.url;
            if (option.target) {
      button.target = option.target;
                } else {
                    button.dataset.next = option.next;
                }
                choices.appendChild(button);
            });
            insertNewChatItem(choices);
        } else if (step.next) {
            printResponse(chat[step.next]);
        }
    };

这会破坏整个代码,因此聊天机器人根本无法运行。

【问题讨论】:

  • 你试过url: "www.google.com target=_blank" 吗?
  • 谢谢 dmikester - 我会检查要更新的问题,但是是的 - 已经尝试过了。它没有破坏代码,但 URL 在同一个选项卡中打开,替换了页面。
  • 为什么没有解释就被否决了?
  • 我不知道那个插件/插件是如何工作的。但我想知道你是否不能这样做:options: [{ text: "PREVIEW THIS LINK", url: "www.google.com", target: "_blank" }]
  • 在某处是否有关于该插件的文档站点?

标签: javascript html url target


【解决方案1】:

我的想法是你必须修改代码或让作者为你修改代码。

我在看这里的主要js代码:https://github.com/Peekobot/peekobot/blob/master/peekobot.js

这个sn-p就是我正在看的:

step.options.forEach(function (option) {
                const button = document.createElement(option.url ? 'a' : 'button');
                button.classList.add('choice');
                button.innerHTML = option.text;
                if (option.url) {
                    button.href = option.url;
                } else {
                    button.dataset.next = option.next;
                }
                choices.appendChild(button);
            });

我想最后一部分会变成这样。

if (option.url) {
   button.href = option.url;
   if (option.target) {
      button.target = option.target;
   }
} else {
...

【讨论】:

  • 为了清楚起见,您建议只添加以下内容: if (option.target) { button.target = option.target;
  • 不幸的是,这不起作用 - 并破坏了整个代码。
  • 我已经在我的问题中发布了您建议的代码的更新:)
  • 您将该代码添加到 bot.js 并破坏了它?你能发布你修改过的 bot.js 吗?
  • 太好了,请务必将此标记为答案。在 SO 上发布一个新问题并将链接粘贴到此处。
猜你喜欢
  • 2013-10-29
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 2014-01-10
  • 2013-06-18
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多