Why performance of javascript is different in firefox opposite to chrome? - javascript

I've write this code:
function validateForm(){
if (document.getElementById('user_login') != null ){
var user_login = document.getElementById('user_login');
if(user_login.value == ''){
user_login.style.backgroundColor = "red";
user_login.focus();
return false;
}
}
}
function goToSec(){
if(validateForm()){
var first = document.getElementById('first');
first.style.display = 'none';
var second = document.getElementById('second');
second.style.display = 'block';//in firfox this line will execute
}
}
in firefox when goToSec() is run by an event, if validateForm() return false the very last code line will be executed but in chrome everything is ok and control of program will go out the block of code.
I tested it with simple script for example:
function f(){
return false;
}
function boo(){
if(f()){
alert('1');
alert('2');//debuger meets this line but the code wont be exeuted
}
}
<button onclick="boo()">Ok</button>
and Debug it with firefox debuger I found out something wonder, this time debuger meet this line but wont execute it
How does firefox work? and How we can handle it?

Related

Can you tell me why this alert does not work?

Can you tell me why this alert does not work?
my js codes:
var character =
document.getElementById("character");
var block = document.getElementById("block");
function jump() {
if (character.classList != "animate") {
character.classList.add("animate");
}
setTimeout(function () {
character.classList.remove("animate");
}, 500);
var checkDead = setlnterval(function () {
var characterTop =
parseInt(window.getComputedStyle(character)
.getPropertyValue("top"));
var blockLeft =
parseInt(window.getComputedStyle(block)
.getPropertyValue("left"));
here is the if part (my problem): i want to know why it dose not work?
if (blockLeft<20 && blockLeft>0 && characterTop>=130) {
block.style.animation = "none";
block.style.display = "none";
alert("U Lose!");
}
}, 10);
}
If there will be two alert boxes when you run the code, then check the "prevent this page from creating additional dialog" checkbox. Refresh the page again and you won't get the alert box ever again.
Solution is that you need to close that webpage and reopen it again in the browser (don't need to close the entire browser). I am assuming you are using Chrome. Internet Explorer or Firefox don't have this checkbox feature.

javascript not operating in safari

I have a funny issue. For some reason on Safari browser the javascript Logic Gates are not working properly. Here is a breakdown:
This whole script is being called with $(#id).load(my/script.php) function
I create a clean vairable.
$perm = $_POST['permission'].
I create html variable
$html_content .= " <div id='bsid_$account_id' onclick="bsview(id)"></div>
I echo the html
<?=$html_content?>
Based on permission ai change the functionality of bsview function i applied in my div.
if ("<?=$perm?>" != "user"){
function bsview(id) {
console.log('manager');
}
}else{
function bsview(id) {
console.log('user_view');
}
}
Then something funny happens. After checking in Chrome, IE, and Firefox I confirm that the code is working. However when I run the code in Safari the logic gate that should return "TRUE" this permission doesn't equal 'user', instead returns FALSE.
This is code echoed into my Safari debugging thinger.
if ("manager" !== "user"){
console.log('manager');
function bsview(id) {
console.log('manager');
}
}else{
function bsview(id) {
console.log('user_view');
}
}
</script>
And of course it console logs 'users_view'
Ok. If anyone has any insight or criticism for my crappy code please send them my way. I would love to know why this is happening, and how to avoid this same thing from happening.
According to
Function declarations inside if/else statements?
it's not valid to put function declarations inside if/else. You should declare the function name outside the if, and then assign to it.
var bsview;
if ("<?=$perm?>" != "user"){
bsview = function (id) {
console.log('manager');
}
}else{
bsview = function (id) {
console.log('user_view');
}
}
Or you could simplify it to:
function bsview(id) {
console.log('<?= $perm == "user" ? 'user_view' : 'manager' ?>');
}

"Unable to load http://url status:0" error in onbeforeunload-method

may someone of you can help me to find this problem?
I've got an xpage with client-side js-code included which should be executed when you decide to leave the page. In the client-side js you refer to a button and click it automatically. This button got some server-side js code included and change the flag from a document from ("opened by ..." to "").
The thing is that somehow the client-side js did not work in all different browsers except the current IE (10.0.5) and throws the error:
unable to load http://urlofthedocument/... status:0
The funny thing about this is, when I insert an alert()-method right after the click()-method everything works fine in every browser. But as I don't want to include this alert statement I figure out there must be something different to avoid this. (A short pause instead of the alert-method also did not work.)
My CS JS-Code:
window.onbeforeunload = WarnEditMode;
function WarnEditMode(){
if(needUnloadConfirm == true){
var el = window.document.getElementById("#{id:Hidden4SSJS}");
el.click();
//document.open();
//document.write(el);
//document.close();
//alert("You're about to leave the page");
//pause(5000);
}
}
function pause(millis){
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < millis)
}
This refers to to button, which executes following SS JS code, after it is clicked:
try{
print("Hidden4SSJS-Button-Test # Person");
var db:NotesDatabase = database;
var agt:NotesAgent;
var doc:NotesDocument = XPPersonDoc.getDocument()
agt = db.getAgent("(XPUnlockDocument)");
agt.run(doc.getNoteID());
}catch(e){
_dump(e);
}
May you guys can help me with this?
I would do this using the XSP object with a hidden computed field (and not your special button)...
Something like this:
function WarnEditMode(){
if(needUnloadConfirm == true){
XSP.partialRefreshGet("#{id:unlockDocCF1}", {
params: {
'$$xspsubmitvalue': 'needToUnlock'
},
onComplete: function () {
alert('You are about to leave this page and the document has been unlocked.');
},
onError : function (e) {
alert('You are about to leave this page and the document has NOT been unlocked.\n' + e);
}
);
}
pause(5000);
}
Then the computed field's javascript would be something like this:
try{
var sval = #Explode(context.getSubmittedValue(), ',');
if (sval == null) return result + " no action.";
if (!"needToUnlock".equals(sval[0])) return result + " no action.";
print("Hidden4SSJS-Button-Test # Person");
var db:NotesDatabase = database;
var agt:NotesAgent;
var doc:NotesDocument = XPPersonDoc.getDocument()
agt = db.getAgent("(XPUnlockDocument)");
agt.run(doc.getNoteID());
return 'document unlocked.';
}catch(e){
_dump(e);
}

Why is wrong with this JS function? Firefox does work but Chrome doesn't

Hi, I have been coding a little Js function that manipulates certains divs and elements. On firefox it works great, but it does not work in Chrome and halts all the Javascript. I simply don't find what is wrong. Could you be kind enough to let me know? Cookies were tested and work fine. Using Jquery. Thanks!
function RememberMe(addr, bycookie = false)
{
// Cookie name
cookiename = "LBETS";
// Should we reset?
reset = false;
changed = false;
// See if button pressed
if($(".star_"+ addr).hasClass("active"))
{
$('#recent_tx').addClass("table-striped");
$(".star_"+ addr).removeClass("active");
reset = true;
} else {
$(".favstar").removeClass("active");
}
// Iterate rows
$('#recent_tx tr').each(function(){
if($(this).hasClass(addr))
{
if(reset)
{
$(this).removeClass('warning');
} else {
$(this).addClass('warning');
changed = true;
}
} else {
$(this).removeClass('warning');
}
})
// Change class
if(changed)
{
$('#recent_tx').removeClass("table-striped");
$(".star_"+ addr).addClass("active");
setCookie(cookiename, addr, 20*365);
}
// Reset
if(reset)
{
delCookie(cookiename);
}
}
Invalid syntax:
function RememberMe(addr, bycookie = false)
You can't do assignment of defaults in the function signature.
Firefox's JS engine allows this syntax, and it is likely coming in some form to ECMAScript 6.

Function not defined message in JavaScript

I am building a game in JavaScript along with html5. I want my front page to display a bit of text and then when I press any button suppose if p then the actual game starts.
I have used flag in this case to toggle between two functions one which brings the front page and another which is actually starting the game.
Here's my code:
var flag1 =true;
function init()
{
canvas = document.getElementById('canvas');
region = canvas.getContext('2d');
if(flag1==true)
{
front();
}
else
{
start();
}
}
function front()
{
document.write("press p to play");
document.onkeydown = function(event)
{
var keyCode;
if(event == null)
{
keyCode = window.event.keyCode;
}
else
{
keyCode = event.keyCode;
}
switch(keyCode)
{
case 80:
flag1=false;
init();
break;
default:
break;
}
}
}
function start()
{
*************
*********
*********
}
This code is giving an error in console whenever I press p, i.e.
init() is not defined
Can anyone sort this problem?
Answer: http://jsfiddle.net/morrison/HrRD6/
Notes:
There's no point in finding canvas again if you've already found it. Storing the canvas to lib.canvas and not retrieving it again seems to be what fixed the problem.
Use library-style functions. This helps avoid naming conflicts.
Tidy your code. I use jsfiddle a lot, so I run it through there, or you can try formatjavascript or javascriptbeautifier.
init() runs for me in chrome. Which browser are you using? What scope is the code in?
Also, as far as I know, JavaScript might be misparsed if it is not indented like so:
if (bool) {
// stuff
}
Try running your code through jslint.

Categories

Resources