Command in chat to open new javascript page

Asked

Viewed 62 times

-1

Hello folks I’m doing a chat and would like that when the user type /rules and press enter it opens a new tab with the address of the rules someone could help me with the code?

 <textarea type="text" name="emsg" id="emsg" maxlength="40" rows="1" placeholder="Digite sua mensagem"></textarea>
  • I’m sorry, I don’t understand. I could describe what you intend to do ?

2 answers

0

You can try this way by typing /regras will automatically open a new tab

<script>
    document.addEventListener('keyup', function (event) {
        var emsg = $("#emsg").val();

        $("#emsg").focusout( function(){
             if(emsg == "/regras") {
                  window.open('www.google.com', '_blank');
             }
        });
    });
</script>
  • I tested here and only opens the new tab if I type /rules in the text area and click out, I would like that by pressing enter with the /rules in the text area it would open the desired page =/

  • can configure a way here but after I type /rules and give enter it opens the new page but any letter I put in the textarea it will open me a new page... =/

0


Good evening, use the jquery function keypress keycode = 13 for enter. The function .slice(-8) will retrieve the last 8 characters typed for the "/rules check"

<!DOCTYPE html>
<html>
<head>
    <title>Exemplo</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
<script>
$( document ).ready(function() {
    $("#emsg").keypress(function (e) {
        var key = e.which;
        if(key == 13) {
            console.log($("#emsg").val().slice(-8));
            if ($("#emsg").val().slice(-8) == "/regras") {
                window.open('www.google.com', '_blank');
            }
        }
    });
});
</script>
<body>
    <textarea id="emsg"></textarea>
</body>
</html>

Remember that when using jquery you should put your methods inside $( document ).ready(function() {});

  • Perfect, thank you very much!!!!

  • By chance you know how I exchange my textarea for an input and that it does not give refresh on the title/link of the site?

  • It can be easily exchanged for an input type=text keeping the same id. refresh didn’t understand you...

  • is that I am using a chat and instead of input I am using textarea and I would like to change, but when I input it kind of "refresh" the link site, getting the input id and =, ex: site.com/store? emsg= I wish he wouldn’t be like this =/

  • Does this happen when you type in input? if yes probably some other script is doing this unwanted behavior.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.