Is there a way to dynamically assign an array? The code doesn't work as intended. It fails at var p = { z }
var z = "\"" + m[0] + "\"" + " : " + "\"" + (m[0] = m[1]) + "\"";
if(i != u.length - 1){
z = z + ",";
}
var p = {z}
for (var key in p) {
if (p.hasOwnProperty(key)) {
client = Elements.AddNew(key, p[key]);
client.Update();
}
}
Entire code
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
// Read the contents of the file.
Session.Output("Reading file");
ts = fso.OpenTextFile("c:\\temp\\roles.txt", ForReading);
s = ts.ReadAll();
u = s.split('\r\n');
Session.Output(u);
for(i = 0; i < u.length; i++){
m = u[i].split(",");
var z = "\"" + m[0] + "\"" + " : " + "\"" + (m[0] = m[1]) + "\"";
if(i != u.length - 1){
z = z + ",";
}
var p = {
z
}
Session.Output(p);
for (var key in p)
{
if (p.hasOwnProperty(key))
{
client = Elements.AddNew(key, p[key]);
client.Update();
}
}
}
The contents of the file are as follows. It's a comma delimited file.
abc,1
def,2
ghi,3
You can't create an object like that.
You need to use bracket notation as the member operator to do this
var p = {}
p[m[0]] = m[1];
for (var key in p) {
if (p.hasOwnProperty(key)) {
client = Elements.AddNew(key, p[key]);
client.Update();
}
}
I'll assume that:
(m[0] = m[1])
should have been:
(m[0] == m[1])
If you have an array m that is like:
var m = ['a', 'b', 'b'];
you seem to be trying to make an object using the pattern:
var z = { m[0] : (m[0] == m[1]) };
which can be written:
var z = {};
z[m[0]] = m[0] == m[1];
An object can be created from the array using:
var z = {};
for (var i=0, iLen=m.length - 1; i<iLen; i++) {
z[m[i]] = m[i] == m[i+1];
}
Which will create an object like:
{a: false, b: true}
Related
Internet Explorer gives an error ')' is expected while Firefox is running this code fine. According to the Internet Explorer console the error is situated in the first line:
function HTMLtableRows (titles=[] , values=[]) {
How can I fix this problem?
function HTMLtableRows (titles=[] , values=[]) {
var i, j;
var str, strT, strM;
str = '<table class="table">';
str = str + '<tr>';
for (j = 0; j < titles.length; j++) {
str = str + '<th colspan="2"><center>' + titles[j] + '</center></th>';
}
str = str + '</tr>' + '<tr>';
for (j = 0; j < titles.length; j++) {
str = str + '<th>Tijdstip</th>' + '<th>Looptijd</th>';
}
str = str + '</tr>' + '<tr>';
for (j = 0; j < titles.length; j++) {
var a = values[j].split('\r');
strT = ''
strM = ''
for (i = 0; i < a.length; i++) {
var b = a[i].split('=');
if (b[1] != undefined) {
strT = strT + b[0];
strM = strM + b[1] + 'min';
}
if (i < a.length - 1) {
strT = strT + '<br>';
strM = strM + '<br>';
}
}
str = str + '<td>' + strT + '</td>';
str = str + '<td>' + strM + '</td>';
}
str = str + '</tr>';
str = str + '</table>';
return str;
}
IE does not support default parameters.
Just do it like this if you want
function HTMLtableRows (titles , values) {
if (!titles) titles = [];
if (!values) values = [];
console.log(titles);
console.log(values);
}
a1 = [1,2,3];
HTMLtableRows(a1, null);
HTMLtableRows({foo: "bar"}, undefined);
HTMLtableRows(2, NaN);
HTMLtableRows("not empty string", "");
HTMLtableRows(1, 0);
HTMLtableRows(true, false);
All of the following values
null
undefined
NaN
""
0
false
will become an empty array. If you don't want some of those values to be overwritten with an empty array adjust if conditions as you see fit.
Example where you allow values to remain unchanged as "", 0 and NaN:
function HTMLtableRows (titles , values) {
if (!titles) titles = [];
if(values != "" &&
values != 0 &&
!isNaN(parseInt(values))
)
values = [];
console.log(titles);
console.log(values);
}
HTMLtableRows("string", "");
HTMLtableRows(1, 0);
HTMLtableRows(7, NaN);
Thanks to alex i've solved this with the adjustments below:
function HTMLtableRows (tmpTitles , tmpValues) {
titles=[];
values=[];
titles = tmpTitles;
values = tmpValues;
//...
}
I want this code to replace an existing URL parameter "aspid", but what it does is adding an another id on the existing one. Can anyone help?
$(document).ready(function() {
function GetUrlValue(VarSearch) {
var SearchString = window.location.search.substring(1);
var VariableArray = SearchString.split('&');
for (var i = 0; i < VariableArray.length; i++) {
var KeyValuePair = VariableArray[i].split('=');
if (KeyValuePair[0] == VarSearch) {
return KeyValuePair[1];
}
}
}
var asid = GetUrlValue('aspid');
var campaign = GetUrlValue('utm_campaign');
if (asid != undefined) {
$("a").attr('href', function(i, h) {
return h + (h.indexOf('?') != -1 ? "&aspid=" + asid : "?aspid=" + asid);
});
}
});
You need to call this function on every a:
/**
* http://stackoverflow.com/a/10997390/11236
*/
function updateURLParameter(url, param, paramVal){
var newAdditionalURL = "";
var tempArray = url.split("?");
var baseURL = tempArray[0];
var additionalURL = tempArray[1];
var temp = "";
if (additionalURL) {
tempArray = additionalURL.split("&");
for (var i=0; i<tempArray.length; i++){
if(tempArray[i].split('=')[0] != param){
newAdditionalURL += temp + tempArray[i];
temp = "&";
}
}
}
var rows_txt = temp + "" + param + "=" + paramVal;
return baseURL + "?" + newAdditionalURL + rows_txt;
}
And call the function like so:
updateURLParameter(window.location.href, 'paramName', 'Value')
complete code be like :
...
$("a").attr('href', function(i, h) {
if(h){
return updateURLParameter(h, 'aspid', asid);
}
});
...
I created a list of buttons from code behind, and append them on some div, how ever each button has an onclick java script function
here is how I did it:
string[] messages = CM.GetMessages(Session["USER_EMAIL"].ToString()).Split(new string[] { "$STARTCHAT$" }, StringSplitOptions.None);
string[] usersalone = CM.GetChaters(Session["USER_EMAIL"].ToString()).Split(new string[] { "$NEWUSER$" }, StringSplitOptions.None);
string[] username = CM.GetUserNames(Session["USER_EMAIL"].ToString()).Split(new string[] { "$NEWUSER$" }, StringSplitOptions.None);
for (int i = messages.Length-2; i>=0; i--)
{
Button b = new Button();
b.ID = Session["USER_EMAIL"].ToString()+username[i];
b.Text = "Chat With: " + usersalone[i] ;
b.Width = 250;
b.Height = 100;
b.OnClientClick = "return DisplayMessage('" + messages[i+1] + "','" + username[i] + "','" + Session["USER_EMAIL"].ToString() + "')";
b.Style.Add("background-color", "rgb(246, 246, 246)");
// lblChatwith.Text = username[i];
NewMsgNotArrow.Controls.Add(b);
}
and here is my java script function:
function DisplayMessage(messages, from, username) {
document.getElementById("AllMessages").innerText ="";
document.getElementById("DivDisplayMessage").style.visibility = "visible";
document.getElementById("lblChatwith").innerText = from;
var MessageForEachUser = messages.split("$SAMECHATNEWTEXT$");
for (var i = 0; i < MessageForEachUser.length; i++)
{
var ck = MessageForEachUser[i].indexOf("$" + from.toUpperCase() + "$") > -1;
if (ck == true) {
document.getElementById("AllMessages").innerText += from.toUpperCase() + ":\n";
var temp = MessageForEachUser[i].split("$" + from.toUpperCase() + "$");
MessageForEachUser[i] = temp[0];
}
if (ck == false) {
document.getElementById("AllMessages").innerText += username.toUpperCase() + ":\n";
var temp = MessageForEachUser[i].split("$" + username.toUpperCase() + "$");
MessageForEachUser[i] = temp[0];
}
document.getElementById("AllMessages").innerText += MessageForEachUser[i] + "\n______________________________________________________" + "\n";
}
return false;
}
every thing is working well but, when i want to use one of the labels like "lblchatwith" from code behind it return an empty string.
I'm creating a text based game with Javscript and I am having some issues with referencing a JSON array.
var cRecipes = [];
function craftItem(id){
var n = cRecipes[id].name;
var t = cRecipes[id].type;
var i1 = cRecipes[id].item1;
var a1 = parseInt(cRecipes[id].amount1);
var i2 = cRecipes[id].item2;
var a2 = parseInt(cRecipes[id].amount2);
for (i = 0; i < inventory.length; i++){
if (inventory[i].item == i1 && inventory[i].amount >= a1){
var inv1 = parseInt(inventory[i].amount)
parseInt(inventory[i].amount) -= a1;
a1 = 0;
}
if (inventory[i].item == i2 && inventory[i].amount >= a2){
parseInt(inventory[i].amount) -= a2;
a2 = 0;
}
}
if (a1 == 0 && a2 == 0){
if (t == "Hat"){
cHat = products[rId].name;
command(products[rId].effect);
p(n + " crafted");
}
else {
p("Insufficient items");
}
}
}
function loadCrafting(){
cRecipes = [
{"name":"Mega Fedora", "type":"Hat", "item1": "Euphorite", "amount1":4, "item2":"Essence Of Euphoria", "amount2":2, "effect":""},
{"name":"Mega Fedora 2", "type":"Hat", "item1": "Euphorite", "amount1":4, "item2":"Essence Of Euphoria", "amount2":2, "effect":""},
];
c();
p("-- Your Crafting --");
back();
for (cr = 0; cr < cRecipes.length; cr++){
p("<span class='choice' id='c"+ cr + "'>" + cRecipes[cr].name +", " + cRecipes[cr].type + ", " + cRecipes[cr].item1 + " x" + cRecipes[cr].amount1 + ", " + cRecipes[cr].item2 + " x" + cRecipes[cr].amount2 + "</span>");
$("#c" + cr).click(function(){craftItem(cr);});
}
}
When running the script, I am given the following error.
Uncaught TypeError: Cannot read property 'name' of undefined
I have used similar methods with other parts of my game and have had no issues so this
i have implemented this code to display xml in html using javascript
current output
Need something like
Here is my code
function parseXML(R, s) {
var C = R.childNodes;
var str = '';
for (var i = 0; i < C.length; i++) {
var n = C[i];
var f = false;
if (n.nodeType !== 3) {
str += '<br><<span class="nn">' + n.nodeName + '</span>>';
if (n.hasChildNodes()) {
f = true;
str += parseXML(n, s++);
}
str += '</<span class="nn">' + n.nodeName + '</span>>';
} else {
str += '<span class="nv">' + n.nodeValue + '</span>';
}
if (f) {
str += '<br>';
}
}
var str = str.replace(/(<br>)+/g, '<br>');
return str;
}
how i call this
R : xml object
s : initial 0 (i am passing this so that i can display xml as hirarchical view)
Output in second
- is not required
i have post second out as it can be seen while opening xml document in firefox
please ask if any doubt
I solved it myself.
Updated code with the solution
var pre = 0;
function parseXML(R, s) {
var C = R.childNodes;
var str = '';
for (var i = 0; i < C.length; i++) {
var n = C[i];
if (n.nodeType !== 3) {
str += '<br>' + gs(s) + '<b><</b><span class="nn">' + n.nodeName + '</span><b>></b>';
if (n.hasChildNodes()) {
str += parseXML(n, s + 1);
}
if (pre !== 3) {
str += '<br>' + gs(s);
}
str += '<b><</b>/<span class="nn">' + n.nodeName + '</span><b>></b>';
} else {
str += '<span class="nv">' + n.nodeValue + '</span>';
}
pre = n.nodeType;
}
return str;
}