HTML Send button - javascript

HTML Button that I create
I wanted to know if there is something for an HTML button, simulate that the ENTER key was pressed. Since I need a button to send an automatic chat in which I am working. I was able to implement the button, although it still does not perform any action. The messages are sent by pressing the ENTER key, so I wanted to find a way to simulate that action. Thank you
var fncTxMessageKeydown = function (e) {
e = window.event || e;
var keyCode = (e.which) ? e.which : e.keyCode;
if (keyCode == 13 && !e.shiftKey) {
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
var message = elements.txMessage.value.toString().trim();
if (message!=""){
if (gotUid==true) {
EngtChat.sendMessage(message);
elements.txMessage.value = "";
}
else{
//show initialization alert and reconnect socket
document.getElementsByClassName("engt-sheet-header-with-presence")[0].style.padding="7px";
socket.disconnect();
socket.connect();
}
}
return false;
}
};
if (elements.txMessage != undefined) {
elements.txMessage.onkeydown = fncTxMessageKeydown;

Related

Line break when submitting form

I'm having an issue with my chat bot app where a line break is being inserted into the form after the user is pressing the submit button. To clarify, the message is being sent, but then creating a line break so the user has to backspace for the form to be completely empty.
var enableEnterKey = function() {
$(document).keypress(function(e) {
if($('#maxx-message-box').is(':focus') && e.keyCode === 13) {
var message = $('#maxx-message-box').val();
sendMessage(message);
}
});
This is our function. Please help.
You probably need something like:
$("#maxx-message-box").keypress(function (e) {
if (e.keyCode != 13) return;
var message = $("#maxx-message-box").val().replace(/\n/g, "");
if (!!message)
{
sendMessage(message);
$("#maxx-message-box").val("");
}
return false;
});

show javascript message to user using onbeforeunload event only for browser close event

As per the project requirement, on which I am working now, I need to show user a javascript alert only in the event of browser close using javascript. All other page events like url click, button click, F5 key press etc. are to be disregarded. I have tried with the following code but with no use.
var isPostBack = false;
$(function() {
// You would copy this for select and any other form elements I forgot about
$('input').live('click', function() { isPostBack = true; });
$('a').live('click', function() { isPostBack = true; });
document.onkeydown = function(e) { //attach to key down event to detect the F5 key
isPostBack = false;
if (!e) { //Firefox and Safari gets argument directly.
e = window.event;
}
var key = e.keyCode ? e.keyCode : e.which;
try {
if (key == 116) { //F5 Key detected
isPostBack = true;
}
}
catch (ex) { }
}
});
window.onbeforeunload = check;
function check() {
if (!isPostBack) {
// Do your unload code
isPostBack = false;
var strPath = window.location.pathname;
if (strPath.indexOf('CustomerPortal') >= 0) {
alert('Customer, you are leaving our page.');
}
else {
alert('User, you are leaving our page.');
}
return "Are you sure you want to exit this page?";
}
}
Please help to achieve my target requirement with your valuable comments and help.

Jquery capture tab + some key combination

How can I catch, for example, tab+t combination with jQuery? I've found a lot of examples with alt, shift and ctrl, since event object contains special flags in order to understand if, for example, alt was pressed. But there is not such thing for tab.
This should work. It's a bit convoluted and there is likely an easier way, but it works fine.
JSFiddle: https://jsfiddle.net/spybhhxc/
var tabdown = false;
var tdown = false;
$(document).keydown(function(e) {
if(e.which == 9) {
tabdown = true;
}
if(e.which === 84)
{
tdown = true;
}
if(tabdown && tdown)
{
//do your thing
}
});
$(document).keyup(function(e) {
if(e.which == 9) {
tabdown = false;
}
if(e.which === 84)
{
tdown = false;
}
});
This presents a problem though, as once you press tab, the document is unfocused as the tab key navigates to elements in a browser. You would be much better off using something like alt or ctrl which don't interact with the browser.
We can have a tab key pressed [tabPressed] variable which will be set to true on key down and unset the same on its key up event. We will using the tab key pressed[tabPressed] variable to check whether it is in pressed state during the other key press activities. The tab keycode is 9.
jsfiddle link http://jsfiddle.net/e3Lveyj2/
var tabPressed=false;
function handleKeyDown(e) {
var evt = (e==null ? event:e);
if(evt.keyCode == 9){
tabPressed=true;
}
if ((tabPressed) && (evt.keyCode == 84)) {
alert ("You pressed 'Tab+t'")
}
}
function handleKeyUp(e) {
var evt = (e==null ? event:e);
if(evt.keyCode == 9){
tabPressed=false;
}
}
document.onkeydown = handleKeyDown;
document.onkeyup = handleKeyUp;

javascript browser closing

i have an issue regarding browser closing detection.i my asp.net application when user closes browser abruptlly i need to call a logout service which update loginstatus from true to false,i am doing this as below
function Logout() {
var Userid = $("#hdnfuid").val();
var branch = $("#hdnfbranch").val();
var service = LogoutService.Logout(Userid + "," + branch, SucceededCallback);
//alert(service);
}
function SucceededCallback(result) {
}
var isClose = false; ;
//this code will handle the F5 or Ctrl+F5 key
//need to handle more cases like ctrl+R whose codes are not listed here
document.onkeydown = checkKeycode
function checkKeycode(e) {
var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (e)
keycode = e.which;
if (keycode == 116) {
isClose = true;
}
}
function somefunction() {
isClose = true;
}
function doUnload() {
if (!isClose) {
var selection = confirm('window is closing');
if (selection == true)
{var x=confirm("closing browser");
if(x==true)Logout();
else
return false;}
else {
return false;
}
}
but the problem is that when i click cancle of confirm box in that case also browser closes.
i also need to call same service on session_end event of global.asax
please suggest me the way of doing this.
}
but the problem is that

switching default keyevents actions after key combinations

I need to change default actions of two events.
When I press "enter" one action occurs, when I press "shift-enter" another. I need to switch it. I means if I press "enter" than "shift-enter" action occurs. I tried something like this but if doesn't work.
f(evt.keyCode == 13) {
if(!evt.shiftKey){
evt.preventDefault();
evt.shiftKey = true;
var e = jQuery.Event("keydown");
e.which = 13;
e.shiftKey = true;
$(wym._doc).trigger(e);
Is there any way to do it?
Here try this :
$(function() {
$('#myinput').keypress(function(e) {
if(e.shiftKey && e.which == 13) {
alert('shift enter');
} else if(e.which == 13) {
alert('enter');
}
});
});

Categories