How to disable specific keyboard keys on a webpage? - html

I am developing a webpage using Wordpress 3.04, and I'm experiencing the following issue:
In this webpage, I implemented a script that changes the background image every 10 seconds or so. Now, when users press the Left Arrow and Right Arrow keys, it makes the background picture change back and forth accordingly, and messes up the rotation cycle.
This becomes a problem in the Contact Form section of the site, since users that might need to navigate left and right inside each field might end up changing the background pic instead.
I would also like to disable the "Enter" key, to avoid the form being sent if the users are not done writing their message.
I looked around and found this javascript code that didn't work:
document.onkeydown=function DisableCTRL(e)
{
var val=(document.all)?event.keyCode:event.which;
if(parseInt(val)==17)//CTRL
{
alert('Not Allowed!');
window.event.returnValue=false;
}
}
This JS code didn't work either:
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = stopRKey;
Any help will be greatly appreciated. Thanks!

I think, you should call stop() on the passed event, i.e.
document.onkeydown = function(e) {
if (e.keyCode == 17) {
alert('Not Allowed!');
e.stop();
}
};
and maybe use addEventListener(). I am not sure, intercepting the Ctrl key down will actually turn of Ctrl altogether. I guess, you need to intercept cursor left/right with the ctrlKey attribute set. For testing key events, see e.g. http://unixpapa.com/js/testkey.html.
PS. document.all: http://javascript.about.com/od/hintsandtips/a/worst_4.htm

You need to call e.preventDefault() to stop the event from going ahead.. I wrote this function to handle unwanted keys on a site recently:
PreventIllegalKeyPress = function (e) {
if (e.target) t = e.target; //Firefox and others
else if (e.srcElement) t = e.srcElement; //IE
if (e.keyCode == 116) { //prevent F5 for refresh
e.preventDefault();
}
if (e.keyCode == 122) { //F11 leave fullscreen
e.preventDefault();
} else if (e.altKey && e.keyCode == 115) { //Alt + F4
e.preventDefault();
} else if (e.altKey && e.keyCode == 37) { //Alt + left
e.preventDefault();
} else if (e.altKey && e.keyCode == 39) { //Alt + right
e.preventDefault();
} else if (e.ctrlKey && e.keyCode == 82) { //Ctrl + R (reload)
e.preventDefault();
}
if (!(t.tagName == 'INPUT')) {
if (e.keyCode == 13) { //enter
e.preventDefault();
}
if (e.keyCode == 8) { //backspace
e.preventDefault();
}
}};
Note the check for t.tagName == "INPUT". This makes sure that both enter and backspace keys are allowed in input field but no-where else.
Then in $(document).ready, paste the following snippet to call the function:
$(document).keydown(function (e) {
NFS.PreventIllegalKeyPress(e);
});
This works perfectly fine for me.

Related

Only allow to click the submit button, to submit the form

I would like to enter the form ,
when press enter the input box , it will submit the form
$('input[type=text]').keypress(function(event) {
if(event.which == 13) {
//enter pressed
active($(this));
}
});
Test it on android mobile, but the keypress can not detect the keycode
How to block this behavior? Thanks a lot
$("input:text").keypress(function(event) {
if (event.keyCode == 13) {
event.preventDefault();
active($(this));
}
});
There we use preventDefault to block his default behavior.
$('input[type=text]').keydown(function(event) {
var keyCode = event.keyCode || event.which;
if (keyCode == 13) {
active($(this));
}
});

Convert code sniplet gotoandstop to if else coondition

i just want to ask how can i make a code without needing the code sniplet gotoandstop when clicking button, i want to make if else statement where if he click this called "answerb.btn" he will go to the destined frame.
Well im thinking this code
If (answer.btn = onPress) {
gotoAndStop(2);
}
But somehow ita wrong code i dont know what code to use for pressing button. Pls enlighten me ty
You should have an event listener. Here is an example of the way you could to this:
button.addEventListener(MouseEvent.CLICK, onClick);
private function onClick(e:MouseEvent):void
{
//your logic here
this.gotoAndStop(2);
};
Here is an example from one of my projects which required text to be entered into blocks before proceeding, but it used if statements with buttons presses so you could modify it to suit your needs as you wish!
stop();
var names:String;
var id:String;
var supervisor:String;
namein.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
idin.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
supervisorin.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
beginbutton.enabled = false;
function paramChanged(event:TextEvent):void
{
if (namein.text != "" && idin.text != "" && supervisorin.text != "" &&
namein.length >=5 &&
idin.length >=5 &&
supervisorin.length >=5)//add your other fields here
{
beginbutton.enabled = true;
beginbutton.addEventListener(MouseEvent.CLICK,
fl_ClickToGoToAndPlayFromFrame);
}
else
{
beginbutton.enabled = false;
//If something changes that means we now fail the test you will want to disable the button again
}
}
function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void
{
names = namein.text;
id = idin.text;
supervisor = supervisorin.text;
gotoAndPlay(15);
}

How to work with circumflex accent in html textarea?

I have textarea in HTML file and I need to work with circumflex accent, that means a user need to have an option to type characters like:
ŝĝĉ
If I try to type it now, it does nothing (it does not type anything). Where could be problem?
I already use:
<meta charset="UTF-8">
The other not common characters works well, for example:
ěščř
The issue was caused by js:
$(document).keydown(function (e)
{
var keycode1 = (e.keyCode ? e.keyCode : e.which);
if (keycode1 == 0 || keycode1 == 9) {
e.preventDefault();
e.stopPropagation();
}
});
the solution was the change to:
$(document).keydown(function (e)
{
var keycode1 = (e.keyCode ? e.keyCode : e.which);
if (keycode1 == 9) {
e.preventDefault();
e.stopPropagation();
}
});
Now it works well. It is possible to type the characters and it still escape the "tab".

chrome.tabs.onUpdated.addListener() is called twice

I'm writing a chrome extension and in some web sites chrome.tabs.onUpdated.addListener() is called twice.
It mostly happens if i click on a link and not when i type in a URL by myself.
From what i found on the web and from many questions asked on StackOverflow there was a bug on chrome but it was fixed several years ago.
Some people claim it happens if there are several iframes in the page, but in my case there are no iframes in my page.
This is my code:
var storage = chrome.storage.local;
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete' && tab.status == 'complete' && tab.url != undefined) {
storage.get('URLs', function(URLs){
for (var item in URLs)
{
if (tab.url == item)
{
return;
}
else
{
//alert ("tab load complete");
storage.set({URLs: [tab.url]});
chrome.tabs.executeScript(tab.id, {
"file": "flashblocker.js"
}, function () { // Execute your code
console.log("Script Executed .. "); // Notification on Completion
});
}
}
});
}
});
How can i make it run only once?
Thanks.
Use a variable and add a check inside the listener to check the value of this variable before executing the alert. You can do something like this:
var tabUpdated = false;
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete' && tab.status == 'complete' && tab.url != undefined) {
if (!tabUpdated) {
alert("tab load complete");
tabUpdated = true;
}
}
});
But this will fail if the content script is actually loading twice as the tabUpdated variable will again be initialized to false. In that case you can use the chrome's Storage API and store the URL for which the listener has been already invoked. Simply add a check for this, if the URL is there in storage, the alert wont be invoked, else it will invoke. You can then clear the storage at the browser start or close.
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete' && tab.status == 'complete' && tab.url != undefined) {
chrome.local.storage.get('URLs', function(URL's) {
// Iterate through this list here and match with tab.url, if the match is found, just return.
if (url is there in list) {return;}
else {
alert("tab load complete");
chrome.local.set({URLs: [tab.url]});
}
});
}
});
This is just an example of how you can achieve it. Tweak it according to your needs.
I hope this helps.

Chrome and IE doesn't fire "change" event for autocomplete field in Drupal 7

I'm building a form with 2 dependant fields - the first is filtering the second. I'm using Drupal 7 Form API and doing it with '#ajax' property to the first field.
The first field is Drupal autocomplete and may be because of this the "change" event is not triggered. In Firefox it works fine - on Chrome and IE it doesn't.
I tried checking the change event in my js script but it is not triggered at all.
Any ideas?
Make the following changes in the file at /misc/autocomplete.js
change:
Drupal.jsAC.prototype.select = function (node) {
this.input.value = $(node).data('autocompleteValue');
};
to:
Drupal.jsAC.prototype.select = function (node) {
this.input.value = $(node).data('autocompleteValue');
$(this.input).trigger('change');
};
this will allow the change trigger to work when the choice in the autocomplete list is clicked.
In order for the change to trigger when it is selected with arrow keys and pressing enter, you need to add the trigger in the hidePopup function changing:
Drupal.jsAC.prototype.hidePopup = function (keycode) {
// Select item if the right key or mousebutton was pressed.
if (this.selected && ((keycode && keycode != 46 && keycode != 8 && keycode != 27) || !keycode)) {
this.input.value = $(this.selected).data('autocompleteValue');
}
// Hide popup.
var popup = this.popup;
if (popup) {
this.popup = null;
$(popup).fadeOut('fast', function () { $(popup).remove(); });
}
this.selected = false;
$(this.ariaLive).empty();
};
to:
Drupal.jsAC.prototype.hidePopup = function (keycode) {
// Select item if the right key or mousebutton was pressed.
if (this.selected && ((keycode && keycode != 46 && keycode != 8 && keycode != 27) || !keycode)) {
this.input.value = $(this.selected).data('autocompleteValue');
$(this.input).trigger('change');
}
// Hide popup.
var popup = this.popup;
if (popup) {
this.popup = null;
$(popup).fadeOut('fast', function () { $(popup).remove(); });
}
this.selected = false;
$(this.ariaLive).empty();
};
Once you make these changes to the autocomplete.js file, you should be able to call the .change trigger like normal.
Drupal 7 Autocomplete uses the event autocompleteSelect. This encompasses select and change when done with mouse or keyboard. Depending on how you build or alter a form you will need to set the #ajax property in different places. Replace all my_ prefixed values with your code specific variables.
Building the form yourself in code would be:
$form['my_field']['#ajax'] = array(
'event' => 'autocompleteSelect',
'callback' => 'my_callback',
'wrapper' => 'my_wrapper',
);
Using hook_form_FORM_ID_alter() this is:
$form['my_field']['my_language'][0]['target_id']['#ajax'] = array(
'event' => 'autocompleteSelect',
'callback' => 'my_callback',
'wrapper' => 'my_wrapper',
);
Try use "autocompletechange" event. Some like thet code
$('your selector').on('autocompletechange', function() {
code;
});