The problem here is that Google Chrome doesn't seem to work for many of the users of my site. I'm not yet sure if all of them are using Vista, but I know some are. I am using Windows 7 and it works for me. I have made sure they have the latest version of Chrome, and so do I. What might the problem be?
My guess is that it has something to do with encoding the URL, because the function that does not work involves a Javascript POST to another page.
Okey, this is how it looks:
function searchDB() {
$(".transparent_class").show();
strHolder = $('#tbxSearchFor').val().replace("å", "char01").replace("ä", "char02").replace("ö", "char03");
for (i = 0; i < 10; i++)
strHolder = strHolder.replace("å", "char01").replace("ä", "char02").replace("ö", "char03");
vTxtBox = '&vTxtBox=' + encodeURIComponent(strHolder);
vResultHasTo = "&vResultHasTo=1";
vChangedAfter = '&vChangedAfter=' + $('#tbxChangedAfter').val();
vChangedBefore = '&vChangedBefore=' + $('#tbxChangedBefore').val();
vCreatedAfter = '&vCreatedAfter=' + $('#tbxCreatedAfter').val();
vCreatedBefore = '&vCreatedBefore=' + $('#tbxCreatedBefore').val();
if ($('#cbSearchNode').is(':checked')) {
vSearchNode = '&vSearchNode=' + $('#hfSelectedNode').val();
}
else
vSearchNode = '&vSearchNode=false';
if ($('#cbSearchDescription').is(':checked')) {
vSearchDescription = '&vSearchDescription=true';
}
else
vSearchDescription = '&vSearchDescription=false';
if ($('#cbSearchAdminDescription').is(':checked')) {
vSearchAdminDescription = '&vSearchAdminDescription=true';
}
else
vSearchAdminDescription = '&vSearchAdminDescription=false';
if ($('#cbSearchEnvironment').is(':checked')) {
vSearchEnvironment = '&vSearchEnvironment=true';
}
else
vSearchEnvironment = '&vSearchEnvironment=false';
vSearchStatus = '&vSearchStatus=' + $('#ddlSearchStatus').val();
vCurrentSort = "&vCurrentSort=";
if(currentSort == "up" || currentSortColumn != column){
if(column == "1")
vCurrentSort += "namedown";
else if(column == "2")
vCurrentSort += "iddown";
else if(column == "3")
vCurrentSort += "statusdown";
else if(column == "4")
vCurrentSort += "createddown";
else
vCurrentSort += "changeddown";
}
else {
if(column == "1")
vCurrentSort += "nameup";
else if(column == "2")
vCurrentSort += "idup";
else if(column == "3")
vCurrentSort += "statusup";
else if(column == "4")
vCurrentSort += "createdup";
else
vCurrentSort += "changedup";
}
$.get('GridViewService.aspx?updater=' + getTime() + vTxtBox + vResultHasTo + vChangedAfter + vChangedBefore + vCreatedAfter + vCreatedBefore + vSearchNode + vSearchStatus + vSearchDescription + vSearchAdminDescription + vSearchEnvironment + vCurrentSort, function(data) {
$("#searchViewBox").html(data);
$('#searchViewBox input').remove();
theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
$(".transparent_class").hide();
});
}
This URL gets formatted in a bad way when using chrome on some machine, causing an exception in GridViewService.aspx, which leads to the code in the last brackets not being executed.
Related
I'm making a puzzle game where the user uses WASD to move a character up, left, down, right respectively. It works fine at the moment but I was wondering if there was a way to break the code down into more intuitive functions. Below is my code:
function move(e)
{
for (var y = 0; y < mapHeight; y++) {
for (var x = 0; x < mapWidth; x++) {
if (map[y][x] == "#" || map[y][x] == "+") {
break;
}
}
if (map[y][x] == "#" || map[y][x] == "+") {
break;
}
}
var player_x = x;
var player_y = y;
if (e.key == 'w') {
var player_new_x = player_x;
var player_new_y = player_y - 1;
if (moveBox(player_new_x, player_new_y, "up") === false) {
return;
}
if (map[player_new_y][player_new_x] == " " ||
map[player_new_y][player_new_x] == ".") {
if (map[player_new_y][player_new_x] == " ") {
map[player_new_y][player_new_x] = "#";
} else if (map[player_new_y][player_new_x] == ".") {
map[player_new_y][player_new_x] = "+";
}
if (map[player_y][player_x] == "#") {
map[player_y][player_x] = " ";
} else if (map[player_y][player_x] == "+") {
map[player_y][player_x] = ".";
}
}
} else if (e.key == 's') {
var player_new_x = player_x;
var player_new_y = player_y + 1;
if (moveBox(player_new_x, player_new_y, "down") === false) {
return;
}
if (map[player_new_y][player_new_x] == " " ||
map[player_new_y][player_new_x] == ".") {
if (map[player_new_y][player_new_x] == " ") {
map[player_new_y][player_new_x] = "#";
} else if (map[player_new_y][player_new_x] == ".") {
map[player_new_y][player_new_x] = "+";
}
if (map[player_y][player_x] == "#") {
map[player_y][player_x] = " ";
} else if (map[player_y][player_x] == "+") {
map[player_y][player_x] = ".";
}
}
} else if (e.key == 'a') {
var player_new_x = player_x - 1;
var player_new_y = player_y;
if (moveBox(player_new_x, player_new_y, "left") === false) {
return;
}
if (map[player_new_y][player_new_x] == " " ||
map[player_new_y][player_new_x] == ".") {
if (map[player_new_y][player_new_x] == " ") {
map[player_new_y][player_new_x] = "#";
} else if (map[player_new_y][player_new_x] == ".") {
map[player_new_y][player_new_x] = "+";
}
if (map[player_y][player_x] == "#") {
map[player_y][player_x] = " ";
} else if (map[player_y][player_x] == "+") {
map[player_y][player_x] = ".";
}
}
} else if (e.key == 'd') {
var player_new_x = player_x + 1;
var player_new_y = player_y;
if (moveBox(player_new_x, player_new_y, "right") === false) {
return;
}
if (map[player_new_y][player_new_x] == " " ||
map[player_new_y][player_new_x] == ".") {
if (map[player_new_y][player_new_x] == " ") {
map[player_new_y][player_new_x] = "#";
} else if (map[player_new_y][player_new_x] == ".") {
map[player_new_y][player_new_x] = "+";
}
if (map[player_y][player_x] == "#") {
map[player_y][player_x] = " ";
} else if (map[player_y][player_x] == "+") {
map[player_y][player_x] = ".";
}
}
} else {
return;
}
render();
}
Is it possible to make four functions, one for each of the movement keys? Any help would be much appreciated
Yes
It´s possible to do what you want, 4 different functions
But ... you should intercept the events keydown (when the user presses the key) and keyup (when the user releases the key)
As long as the key is "pressed" you do the movement
You can create an object like this
let move = { moveH : 0 , moveV :0 }
When the keydown event is detected for "a" -> {moveH : -1, moveV :0}
"s" -> { moveH :0 , moveV :1 }
"w" -> { moveH :0 , moveV :-1 }
"d" -> { moveH :1 , moveV :0 }
When the keyup event is detected .. for any key -> {moveH :0 , moveV:0 }
Meanwhile
apply the move to the object on the screen
something like
stuff.position = { x : stuff.position.x + move.moveH , y: stuff.position.y + move.moveV }
SOLVED TLDR EDIT: I am stupid and tried to get "message" in the ready event, so yea, look up what the code that you're copy and pasting does lads!
Simply put: I made a discord bot in javascript, checked it with JSHint and then ran it on node.js.
The problem: everytime i ran the bot, i got the "msg is not defined" error
I found already answered questions (Question 1, Question 2) but i looked at the solutions listed in the comments of them and it was already like how the commenters said it should be.
Note: the node.js server is on an android phone, and i already installed discord.js on it.
Source code of my bot:
const Discord = require('discord.js');
const client = new Discord.Client();
var delayed = [];
var funcs = {
clean: function(text) {
var temp = "";
text = text + "";
for (var i = 0; i < text.length; i++) {
if (text.charAt(i) != "_" && text.charAt(i) != "*" && text.charAt(i) != "`" && text.charAt(i) != " ") {
temp = temp + text.charAt(i);
}
}
return temp.toLowerCase();
},
log: function(logthat, cmddude) {
console.log(Date().slice(4, 24) + " | " + logthat + " | " + cmddude + "\n");
},
cook: function(cookthishex) {
cookthishex = cookthishex + '';
var returning = '';
for (var i = 0; i < cookthishex.length; i += 2) {
returning += String.fromCharCode(parseInt(cookthishex.substr(i, 2), 16));
}
return returning;
},
random: function(low, high) {
return Math.round(Math.random() * (high - low)) + low
},
delay: function(userid) {
delayed.push(userid);
setTimeout(function(Argument) {
var temp = delayed[delayed.indexOf(Argument)];
delayed[delayed.indexOf(Argument)] = delayed[delayed.length];
delayed[delayed.length] = temp;
delayed.pop();
}, 1000, userid);
},
split(argtext) {
var splitlist = [], splittext = "";
for (var i = 0; i < argtext.length; i++) {
if (argtext.charAt(i) === " ") {
splitlist.push(splittext);
splittext = "";
} else {
splittext = splittext + argtext.charAt(i);
}
}
if (splittext.charAt(splittext.length - 1) === " ") {
splitlist.push(splittext);
}
return splitlist;
}
};
client.on('ready', () => {
console.log("Logged in as ${client.user.tag}!");
funcs.log("activated");
var reacttocomment = true;
if (client.guilds.get(msg.guild.id).roles.find(x => x.name == "No Bruh") == null) {
client.guilds.get(msg.guild.id).createRole({
name: "No Bruh",
color: "0xf0f0f0",
mentionable: false
}, "Required role to make the 'bruh!doreact' and 'bruh!noreact' commands work.");
}
});
client.on('message', msg => {
if (delayed && msg.author.id !== "492665478687490048") return;
if (msg.author.bot) return;
if (msg.author.id === "489572485126422529") return;
var msgcont = msg.content.slice(13);
if (reacttocomment === true) {
if (funcs.clean(msg.content) === 'bruh' && msg.member.roles.some(r => r.name === "No Bruh") === true) {
delayed.push(msg.author.id);
funcs.log("bruh", msg.author.id);
msg.channel.reply('bruh');
delay(msg.author.id);
}
if (msg.content.slice(0,10) === 'bruh!invite') {
delayed.push(msg.author.id);
funcs.log("invite", msg.author.id);
msg.channel.reply('(invite doesnt exist right now, this bot is currently being tested.)');
delay(msg.author.id);
}
if (msg.content.slice(0, 8) === 'bruh!help') {
delayed.push(msg.author.id);
funcs.log("help", msg.author.id);
msg.channel.reply('`I am "Bruh Bot" [Prefix: bruh!]`\n' +
'`My mission is to reply with "bruh" when someone writes "bruh".`\n' +
'```command list:\n' +
'help: This.\n' +
'invite: Gives bot invite link.\n' +
'noreact: The bot wont react to you with "bruh" if you say it.```\n' +
'random: Get a random number from A to B.```\n' +
'avatar: Display the URL of your avatar.```\n' +
'doreact: The bot will react to you with "bruh" if you say it.```\n' +
'rate: Rates the action/attribute/object you give it.```\n' +
'ping: Get bot "latency".```\n' +
'**`Have a good day (or night)!`**');
delay(msg.author.id);
}
if (msg.content.slice(0, 11) === 'bruh!noreact') {
delayed.push(msg.author.id);
funcs.log("noreact", msg.author.id);
msg.member.addRole('No Bruh');
msg.channel.reply("Won't react to**" + msg.author.tag + '** if he says "bruh".');
if (member.guild.me.hasPermission("MANAGE_ROLES")) {} else {
msg.channel.reply("If I had the 'Manage Roles' permission. :(");
}
delay(msg.author.id);
}
if (msg.content.slice(0, 11) === 'bruh!doreact') {
delayed.push(msg.author.id);
funcs.log("doreact", msg.author.id);
msg.member.removeRole("No Bruh");
msg.channel.reply("Will react to**" + msg.author.tag + '** if he says "bruh".');
if (member.guild.me.hasPermission("MANAGE_ROLES")) {} else {
msg.channel.reply("If I had the 'Manage Roles' permission. :(");
}
delay(msg.author.id);
}
if (msg.content.slice(0, 11) === 'bruh!random') {
delayed.push(msg.author.id);
funcs.log("random "+ msgcont, msg.author.id);
for (var i = 0; i < msgcont.length; i++) {
if (msgcont.charAt(i) === " ") {
var splitpoint = i;
break
}
}
if (splitpoint < 2 || splitpoint > msgcont.length - 2) {
msg.channel.reply("There is an error in your parameters, please try again.");
} else {
msg.channel.reply(funcs.random(msg.content.slice(13, splitpoint - 1), msg.content.slice(splitpoint + 1, msgcont.length)));
}
delay(msg.author.id);
}
if (msg.content.slice(0, 9) === 'bruh!rate') {
delayed.push(msg.author.id);
funcs.log("rate " + msg.content.slice(11), msg.author.id);
var ratethis = "";
if (msg.content.length < 11) {
ratethis = "absolutely nothing";
} else {
ratethis = '**"' + msg.content.slice(11) + '"**';
}
msg.channel.reply('I give ' + ratethis + ' a ' + funcs.random(0, 10) + "/10.");
delay(msg.author.id);
}
if (msg.content.slice(0, 11) === 'bruh!avatar') {
delayed.push(msg.author.id);
funcs.log("avatar", msg.author.id)
msg.channel.reply("Here is your avatar URL:");
msg.channel.reply(msg.author.avatarURL);
delay(msg.author.id);
}
if (msg.content.slice(0, 9) === 'bruh!ping') {
delayed.push(msg.author.id);
msg.channel.reply("Pong! " + new Date().getTime() - msg.createdTimestamp + "ms");
delay(msg.author.id);
}
if (msg.content.slice(0, 8) === 'bruh!rps') {
delayed.push(msg.author.id);
var user = funcs.clean(msg.content.slice(9));
var optiontable = [["Scissors!/nScissors tie with scissors!", "Scissors!/nRock beats scissors!", "Scissors!/nPaper loses to scissors!"], ["Rock!/nScissors lose to rock!", "Rock!/nRock ties with rock!", "Rock!/nPaper beats rock!"], ["Paper!/nScissors beats paper!", "Paper!/nRock loses to paper!", "Paper!/nPaper ties with paper!"]];
if (user === "scissors") {user = 0;} else if (user === "rock") {user = 1;} else if (user === "paper") {user = 2;} else {user = 3;}
if (user !== 3) {
msg.channel.reply(optiontable[funcs.random(0, 2)][user]);
} else {
msg.channel.reply("Please enter either 'Scissors', 'Rock' or 'Paper'");
}
delay(msg.author.id);
}
}
if (msg.author.id === "492665478687490048") {
if (msg.content.slice(0, 13) === 'bruh!norespond') {
funcs.log("norespond", msg.author.id);
reacttocomment = false;
msg.channel.reply('Now not responding to commands and bruhs.');
}
if (msg.content.slice(0, 10) === 'bruh!dummy') {
var dummy = msg.content.slice(11);
}
if (msg.content.slice(0, 14) === 'bruh!serverinfo') {
funcs.log("serverinfo", msg.author.id);
msg.author.send(msg.guild.name + ': {guildID: ' + msg.guild.id + ', guildIconURL: ' + msg.guild.iconURL + ', guildMemberCount: ' + msg.guild.memberCount + ', guildOwnerID: ' + msg.guild.ownerID + ', guildRegion: ' + msg.guild.region + ', guildCreatedAt: ' + msg.guild.createdAt + '}');
}
if (msg.content.slice(0, 13) === 'bruh!dorespond') {
funcs.log("dorespond", msg.author.id);
reacttocomment = true;
msg.channel.reply('Now responding to commands and bruhs.');
}
if (msg.content.slice(0, 11) === 'bruh!execute') {
var rawhex = msgcont;
funcs.log("executing " + rawhex, msg.author.id);
eval(funcs.cook(rawhex));
funcs.log("executed", msg.author.id);
}
if (msg.content.slice(0, 7) === 'bruh!say') {
funcs.log("say " + msg.content.slice(9), msg.author.id);
msg.channel.reply(msg.content.slice(9));
}
}
});
client.login('no');
Your ready event:
client.on('ready', () => {
console.log("Logged in as ${client.user.tag}!");
funcs.log("activated");
var reacttocomment = true;
if (message.guild.roles.find(x => x.name == "No Bruh")) {} else{
msg.guild.createRole({
name: "No Bruh",
color: "0xf0f0f0",
mentionable: false
}, "Required role to make the 'bruh!doreact' and 'bruh!noreact' commands work.");
}
});
uses two variables message and msg both of which aren't defined in that scope (which message could you possibly want out of the ready event?). Consider fetching a guild explicitly if your bot will only ever be on one guild:
client.on('ready', () => {
console.log("Logged in as ${client.user.tag}!");
funcs.log("activated");
var reacttocomment = true;
if (client.guilds.get("someguildID").roles.find(x => x.name == "No Bruh") == null) {
client.guilds.get("someguildID").createRole({
name: "No Bruh",
color: "0xf0f0f0",
mentionable: false
}, "Required role to make the 'bruh!doreact' and 'bruh!noreact' commands work.");
}
});
As an additional note, several places in your code you compare user IDs against number literals e.g. here:
if (delayed && msg.author.id !== 492665478687490048) return;
However, msg.author.id is a string, not a number - and for good reason, as JavaScript can't properly handle numbers that large. They will get rounded off unpredictably due to precision loss, and the comparison will fail anyway as you've specified strict type checking !==. Specify your IDs as strings when making comparisons:
if (delayed && msg.author.id !== "492665478687490048") return;
I have an extremely complex page that uses dynamic information to generate a layout with the correct and relevant information.
I am storing the data as an object. I have essentially 15 objects with multiple fields of user-submitted data.
Everything is stored and output correctly on the page, however now I am trying to validate and the information when the user tries to edit it from the edit page. The information is all being generated and laid out correctly, however I keep getting the same errors on the validation of the information.
The validation should go through and determine if a field was filled out correctly, and if it was not record a variable and add it to an alert variable. Then once it i done running the validation function it should pop up an alert with what fields still need to be filled in.
I keep receiving an error when it runs through the for loop toward the bottom. It says 'Uncaught TypeError' Cannot read property 'questionNumber' of undefined.
Above the code below I store the object and the properties, but this function is where everything is going awry. Note that there are also 15 arrays in the qtest object, but for the sake of simplification I removed all but a few.
I have gotten this to work on smaller, simpler forms, however because of the complexity and storage method I think this may be missing something or I might not be accessing something correctly. The code is very long and below, I've scaled back as much as possible. Please, if you have any insight or help you can provide I would be extremely grateful. Thank you!
var validateQ = function(qTextID, qAnswerType, TFID, MCID, MCText1, MCText2, MCText3, MCText4, VisRef, Youtube, Vimeo, ImgID) {
if (document.getElementById('ItemName').value == "") {
var quizName = true;
};
if (jQuery('select[name="CAT_Custom_14"]').val() == 'Quiz') {
if (jQuery(qTextID).val() == "") {
var qText = true;
};
if (jQuery('CAT_Custom_249').val() == " ") {
var quizscore1 = true;
};
if (jQuery(qAnswerType).val() == " ") {
var answertype = true;
} else if (jQuery(qAnswerType).val() == 'True/False') {
if (!jQuery(TFID).is(':checked')) {
var tfanswer = true;
var mcanswer = false;
};
} else if (jQuery(qAnswerType).val() == 'Multiple Choice') {
if (!jQuery(MCID).is(':checked')) {
var mcanswer = true;
var tfanswer = false;
};
if (jQuery(MCText1).val() == "" || jQuery(MCText2).val() == "" || jQuery(MCText3).val() == "" || jQuery(MCText4).val() == "") {
var mcTextfields = true;
} else {
mcTextfields = false;
};
};
} else if (jQuery('select[name="CAT_Custom_14"]').val() == 'Survey') {
if (jQuery(qTextID).val() == "") {
var qText = true;
};
if (!jQuery(sAnswers1).is(':checked')) {
var surveyAnswers1 = true;
} else {
surveyAnswers1 = false;
};
};
if (jQuery(VisRef).val() != " ") {
if (jQuery(VisRef).val() == "Youtube Video" && jQuery(Youtube).val() == "") {
var youtubeVal = true;
} else if (jQuery(VisRef).val() == "Vimeo Video" && jQuery(Vimeo).val() == "") {
var vimeoVal = true;
} else {
// validateImage(ImgID);
};
} else {
youtubeVal = false;
vimeoVal = false;
var tempImgCheck = false;
};
if (numCheck == 15) {
numCheck = 16;
};
var qName = "- Quiz or Survey Name\n";
var shortDescription = "- A short description of the Quiz/Survey\n";
var scoreMessage = "- A required passing score\n";
var QTextMessage = "- Question text\n";
var answerTMessage = "- An answer type\n";
var mcFields = "- The Multiple Choice answer fields\n";
var mcMessage = "- The correct Multiple Choice Answer\n"
var tfMessage = "- The correct True/False answer\n";
var vimMessage = "- A Vimeo Video code\n";
var ytMessage = "- A Youtube Video code\n";
var imgMessage = "- A reference image\n";
var surveyMessage = "- An answer type\n";
if (quizName == true || quizscore1 == true || qText == true || answertype == true || tfanswer == true || mcanswer == true || mcTextfields == true || youtubeVal == true || vimeoVal == true || tempImgCheck == true || surveyAnswers1 == true) {
var alertText = "It appears that you have not finished completing question" + question[i].questionNumber + ". Please ensure that you have completed the following question fields.\n";
if (quizName == true) {
alertText = alertText + qName;
};
if (quizscore1 == true) {
alertText = alertText + scoreMessage;
};
if (qText == true) {
alertText = alertText + QTextMessage;
};
if (answertype == true) {
alertText = alertText + answerTMessage;
};
if (tfanswer == true) {
alertText = alertText + tfMessage;
};
if (mcanswer == true) {
alertText = alertText + mcMessage;
};
if (mcTextfields == true) {
alertText = alertText + mcFields;
};
if (youtubeVal == true) {
alertText = alertText + ytMessage;
};
if (vimeoVal == true) {
alertText = alertText + vimMessage;
};
if (tempImgCheck == true) {
alertText = alertText + imgMessage;
};
if (surveyAnswers1 == true) {
alertText = alertText + surveyMessage;
};
if (quizscore1 == true) {
alertText = alertText + scoreMessage;
};
confirm(alertText);
};
};
var numCheck = 1;
var checkQuizQ = function() {
for (j = 1; j<= qtest.length; j++) {
numCheck = numCheck + 1;
if (qtest[j].questionNumber == "1") {
validateQ("CAT_Custom_3", "CAT_Custom_8", "CAT_Custom_19", "CAT_Custom_18", "CAT_Custom_4", "CAT_Custom_5", "CAT_Custom_6", "CAT_Custom_7", "CAT_Custom_9", "CAT_Custom_10", "CAT_Custom_11", "CAT_Custom_12", "CAT_Custom_230");
} else if (qtest[j].questionNumber == "2") {
validateQ("CAT_Custom_20", "CAT_Custom_21", "CAT_Custom_29", "CAT_Custom_26", "CAT_Custom_22", "CAT_Custom_23", "CAT_Custom_24", "CAT_Custom_25", "CAT_Custom_30", "CAT_Custom_31", "CAT_Custom_32", "CAT_Custom_33", "CAT_Custom_231");
} else if (qtest[j].questionNumber == "3") {
validateQ("CAT_Custom_35", "CAT_Custom_36", "CAT_Custom_37", "CAT_Custom_40", "CAT_Custom_41", "CAT_Custom_42", "CAT_Custom_43", "CAT_Custom_44", "CAT_Custom_45", "CAT_Custom_46", "CAT_Custom_47", "CAT_Custom_48", "CAT_Custom_232");
} else if (qtest[j].questionNumber == "4") {
};
};
document.getElementById('catcustomcontentbutton').style.display = "block";
document.getElementById("qsValidate").style.display = "none";
};
Since qtest looks like an array, its index starts from 0 to length - 1 so when j is length the value of qtest[j] will be undefined.
So change the loop as
for (j = 0; j< qtest.length; j++) {
//
}
I have never used cookies before, so I am using a peice of code I am very unfamiliar with.
It was working all fine, until I noticed just now that for select boxes, it is not working for any values after the tenth index. (for index 10 and above).
I have looked at the cookie stored on my system, and it appears t be saving them correctly. (I saw select10) ETC stored properly.
When it runs onload of body however, it is not loading in the values properly.
Here is the cookie code I am using:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var expDays = 100;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) { endstr = document.cookie.length; }
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
// use the following code to call it:
// <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">
function cookieForms() {
var mode = cookieForms.arguments[0];
for(f=1; f<cookieForms.arguments.length; f++) {
formName = cookieForms.arguments[f];
if(mode == 'open') {
cookieValue = GetCookie('saved_'+formName);
if(cookieValue != null) {
var cookieArray = cookieValue.split('#cf#');
if(cookieArray.length == document[formName].elements.length) {
for(i=0; i<document[formName].elements.length; i++) {
if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[formName].elements[i].checked = true; }
else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }
else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }
}
}
}
}
if(mode == 'save') {
cookieValue = '';
for(i=0; i<document[formName].elements.length; i++) {
fieldType = document[formName].elements[i].type;
if(fieldType == 'password') { passValue = ''; }
else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }
else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }
else { passValue = document[formName].elements[i].value; }
cookieValue = cookieValue + passValue + '#cf#';
}
cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter
SetCookie('saved_'+formName, cookieValue, exp);
}
}
}
// End -->
</script>
I beleive the problem lies with the following line, found about 3/4 of the way down the code block above (line 68):
if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
Just for reference, here is the opening body tag I am using:
<body style="text-align:center;" onload="cookieForms('open', 'ramsform', 'decksform', 'hullsform', 'crewform', 'shipwrightform'); Swap(crew0z,'crew0i'); Swap(crew1z,'crew1i'); Swap(crew2z,'crew2i'); Swap(crew3z,'crew3i'); Swap(crew4z,'crew4i'); Swap(crew5z,'crew5i'); Swap(crew6z,'crew6i'); Swap(crew7z,'crew7i'); Swap(crew8z,'crew8i'); Swap(crew9z,'crew9i');" onunload="cookieForms('save', 'ramsform', 'decksform', 'hullsform', 'crewform', 'shipwrightform');">
(Please ignore the swap()'s as they are unrelated)
Page I am working on can be found: http://webhostlet.com/POP.htm
In both the open and save codes, change:
document[formName].elements[i].options.selectedIndex
to:
document[formName].elements[i].selectedIndex
options is an array of all the options, the selectedIndex property belongs to the select element that contains them.
Change:
cookieArray[i].substring(7, cookieArray[i].length-1)
to:
cookieArray[i].substring(6)
You were off by 1 because you forgot that it's 0-based counting. The second argument isn't needed, it defaults to the rest of the string.
The reason it worked for the first 10 menu items is a quirk of substring: if the second argument is lower than the first, it swaps them! So "select5".substring(7, 6) is treated as "select5".substring(6, 7), which gets the last character of the string. But for the longer strings, it was `"select35".substring(7, 7), which is an empty string.
OK so this works on all and every browser ive tried it on, but when i try it with internet explorer, its like i dont even have the CheckForm Action there. Any help at all would be awesome. Here is the Script.
function MM_preloadImages() { //v3.0
var d = document;
if (d.images) {
if (!d.MM_p) d.MM_p = new Array();
var i, j = d.MM_p.length,
a = MM_preloadImages.arguments;
for (i = 0; i < a.length; i++)
if (a[i].indexOf("#") != 0) {
d.MM_p[j] = new Image;
d.MM_p[j++].src = a[i];
}
}
}
function checkForm() {
var errors = "";
if (isEmpty("Name")) {
errors += "- Name missing\n";
}
if (isEmpty("Email")) {
errors += "- Email missing\n";
}
if (isEmpty("Phone")) {
errors += "- Phone missing\n";
}
if (isEmpty("Dateneed")) {
errors += "- Date Needed Missing\n";
}
if (isEmpty("ZipCode")) {
errors += "- Zip code mising\n";
}
if (errors.length != 0) {
errors += "\n";
}
var rad_val = document.form1.LanyardStyle.value;
var quantity = parseInt(document.form1.Quantity2.value);
if (isNaN(quantity)) {
quantity = 0;
}
if (rad_val == 'Polyester' && quantity < 100) {
errors += "- Minimum order for Polyester is 100";
}
else if (rad_val == 'AntiMicro' && quantity < 100) {
errors += "- Minimum order for AntiMicro is 100";
}
else if (rad_val == 'Bamboo' && quantity < 100) {
errors += "- Minimum order for Bamboo is 100";
}
else if (rad_val == 'PET' && quantity < 100) {
errors += "- Minimum order for PET is 100";
}
else if (rad_val == 'Reflective' && quantity < 100) {
errors += "- Minimum order for Reflective is 100";
}
else if (rad_val == 'Dyesub' && quantity < 200) {
errors += "- Minimum order for Dyesub is 200";
}
else if (rad_val == 'Woven' && quantity < 500) {
errors += "- Minimum order for Woven is 500";
}
if (errors.length > 0) {
alert("Information missing or invalid:\n\n" + errors);
return false;
}
return true;
}
function getText(id) {
return document.getElementById(id).value.trim();
}
function isEmpty(id) {
if (getText(id).length == 0) {
return true;
}
return false;
}
Try this:
function checkForm() {
var rad = document.form1.LanyardStyle.value,
quantity = parseInt(document.form1.Quantity2.value, 10) || 0,
errors = '',
fields = ['Name', 'Email', 'Phone', 'Dateend', 'ZipCode'],
min = {'Polyester':100, 'AntiMicro':100, 'Bamboo':100, 'PET':100,
'Reflective':100, 'Dyesub':200, 'Woven':500};
for (var i = 0, l = fields.length; i < l; i++) {
if ( isEmpty(fields[i]) ) {
errors += '- ' + fields[i] + ' missing\n';
}
}
if ( quantity < min[rad] ) {
errors += '- Minimum order for ' + rad + ' is ' + min[rad];
}
if ( errors ) {
alert('Information missing or invalid:\n\n' + errors);
return false;
}
return true;
}