So, here's my code:
localStorage.setItem("nhac",for(p=0;p<opener.document.getElementsByName('link').length;p++){opener.document.getElementsByName('link').item(p).style.backgroundColor})
I'm so confused i can't think anymore xD, I've been trying all day long to make this work, i can't even explain it,sorry.
I have a popup which i opened from the main page. From the pop up window, im retrieving the elements from which i want to save the background-color so, by saving it in localstorage, it will always remain like that.
I've done others that work well, like this
localStorage.setItem("color_chosen2", opener.document.getElementById('header').style.color);
I don't know why it doesn't work, can anyone help me? It gives me some kind of error( the first one)
thanks
Edit:(sorry, I was in a hurry when i posted this)
The error it gives is "SyntaxError: syntax error"
(I'm using firefox, by the way)
Using the javascript console, this code
for(p=0;p<opener.document.getElementsByName('link').length;p++){opener.document.getElementsByName('link').item(p).style.backgroundColor}
returns the background color of the elements perfectly, but when I try to use it as the value in localStorage , it gives an error.
And no, there's no problem with the syntax localStorage.setItem, it's how it is, thanks.
Edit2: if you need to see how it works better, here's my website. the pop-up opens from the link in the bottom center.
You are trying to set a for loop into a setter which accepts a String. You must set each of the styles individually and with a unique key value.
Also, I don't think item is a method...
Try this...
items = opener.document.getElementsByName('link');
for ( p = 0; p < items.length; p++ ){
localStorage.setItem( p + 'nhac', items[p].style.backgroundColor );
}
Well, noone really effectively helped me, but at least 2 people tried to help, so thank you.
Anyway, i don't know how, I had an idea and the code works now...to anyone interested:
if(localStorage.getItem("color_chosen3")){
for(p=0;p<document.getElementsByName('link').length;p++){document.getElementsByName('link').item(p).style.backgroundColor=localStorage.getItem("color_chosen3")}}}
Problem solved, question closed...whatever. thanks
Related
I'm trying to get this working off a branch of someone else's code, but wanted to take a hack at it, and so far, am not sure where the javascript is defined, etc. Not much knowledge here, but from what I'm understanding, this is suboptimal. I've tried a thing or two, but it breaks the whole page.
The TimeZone seems to display incorrectly: https://i.imgur.com/2OJrQuC.png
And the Javascript is here: https://github.com/mattgphoto/status/blob/master/script.js
It's something to do with formatDate, but am not sure what of the sections needs to be corrected, or if it's all of them. Would someone be able to help?
I think the following line in formatDate function is causing the problem.
return y.replace(/(y+)/g, function (v) {
return x.getFullYear().toString().slice(-v.length)
});
Try replacing that code with
return y;
Hope it works!
I'm really stuck on this javascript question!
So I'm making a web page that will be completely animated (so it can be used for display for example in a television). That animation will be configurable by the user (stored in a database).
Right now, I've already made the code to store the configuration and to get the configuration (I do an AJAX call and save the configuration in an array of json objects) and everything is as it should be.
The problem is in the animation in which I go through the array and use setTimeout function to create animations. To iterate through the array I rotate it
(I use array.push(array.shift()) according to the answer here).
The first time the intervalmaster function is used, everything goes according to plan, but when the function is called again I need to rotate the array once more (because of the last animation) and the array just doesn't rotate!
Bellow I've left a portion of the code that I'm using that reproduces the problem I'm getting. I've also added the array jsonanima with some possible values (In reality the array is probably much bigger and with higher values).
I really don't understand what is happening, I've also considered that this could be a problem of the multiple setTimeout functions because I've read somewhere (couldn't find the link, sorry!) that is not really advised to use multiple setTimeout.
If that's the case is there any other way to do this?
Thank you in advance!
EDIT: Thanks to the comment from mplungjan I've realized that if change the console.log(jsonanimate) to console.log(JSON.stringfy(jsonanima)) it outputs the correct values (the json array rotated). This got me even more confused! Why do I need to JSON.stringfy to get the array in the correct order?!
Anyway, can't test this with the full code now as I'm not in the office, tomorrow I'll give more feedback. Thank you mplungjan.
EDIT2: Finally solved my problem! So the thing was the call to the function recursivegroup (recursivegroup(0);), this call was made before I rotated the array, so when restarting the animation the array would still have the incorrect values and every sub-sequential value was wrong.
A special thanks to mplungjan and trincot for the comments that helped me debug this problem.
Bellow I leave the code corrected so anybody with the same problem can check it out.
jsonanima=[{"VD":5,"A":10,"diff":0.25},{"L":7,"IE":8,"diff":0.25}];
function intervalmaster(flag){
function recursivegroup(index)
{
if(index==0)
{
//animateeach(jsonanima,0);
}
setTimeout(function(){
//HERE IT WORKS
jsonanima.push(jsonanima.shift());
console.log(JSON.stringify(jsonanima));
//animateeach(jsonanima,0);
//removed the if statement, since it was irrelevant as mplungjan noted
recursivegroup(index+1);
},(jsonanima[0]['diff'])*60*1000);
}
//Changed this
//recursivegroup(0);
var mastertime=0;
for(var key in jsonanima)
{
mastertime+=(jsonanima[key]['diff']);
}
console.log(mastertime,flag);
console.log(JSON.stringify(jsonanima));
if(flag==true)
{
jsonanima.push(jsonanima.shift());
console.log(JSON.stringify(jsonanima));
}
//changed to here
recursivegroup(0);
masterinterval=setTimeout(function(){intervalmaster(true)},mastertime*60*1000);
}
intervalmaster(false);
in my code I was always doing
for(i in vector)...
and it always worked, but the problem is that it somehow changed and now my for shows all the values but also the properties, like "remove" that is a function, and it is breaking my whole code.
I don't know why it suddenly changed, because I didn't do anything and I'm getting crazy with this already.
Do you guys know what is happening with my application?
Another thing is that the code only get this problem on my computer.
If I clone my repository again and try it works for while but then starts the problem again.
Thank you.
The in operator has always had this behaviour. Just check that the property exists directly on the object instead of on the prototype:
for (var i in vector) {
if (vector.hasOwnProperty(i)) {
// Property exists on object
}
}
That should solve your issues.
Tom
I am trying to fill in a text-field on a website for which the id changes every time the website is opened. To circumvent this, I look up the webelement by it's class name and this seems to work well as I am able to click on it through
currentWebElement.Click();
however, when I try to fill in the edit text box through
currentWebElement.SendKeys("51");
nothing happens (even more so, focus is lost)
I have searched a lot to see what the problem is but have not been able to find a solution so I was wondering if someone here can point me in the right direction. What I have tried so far:
1) I was working in chrome but since this seems to be a common problem with sendKeys I have swithed to ie, alas the problem persisted. I have tried migrating to firefox but did not succeed as I cannot seem to locate the binary path to firefox.exe but this should be addressed in a different question. So the problem occurs both in chrome and IE.
2) Using javascript to send the command as I have found this suggestion in other questions that were answered on this website:
driver.ExecuteScript("arguments[0].value = '51'", currentWebElement);
directly but also implementing it as a function did not work:
private static object setValue(this IWebDriver driver, IWebElement element, String value) {
return ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].value = arguments[1]", element, value);
}
// and then calling it in my code:
setValue(driver, currentWebElement, "51");
all implementations did not give me errors nor warnings (I managed to get rid of those during the process), but none of them give the desired result i.e. the box keeps remaining empty which is really starting to bug me!
Try the below code. I have used XPATH to sendKeys in the field(FYI the PRMT_TB part of the id doesn't change, even if the rest part does each time, hence the xpath):
var currentWebElement = driver.FindElement(By.XPath("//input[starts-with(#id,'PRMT_TB')]"));
currentWebElement.SendKeys("51");
OR
In case the above doesn't work, please try the below code too
var currentWebElement = driver.FindElement(By.XPath("//td[#class='clsTextWidgetParseError']/input"));
currentWebElement.SendKeys("51");
By byCss = By.CssSelector(".clsTextWidgetParseError>input");
var element = Driver.FindElement(byCss );
element.Clear();
element.SendKeys(value);
I am assuming you are using wrong selector. If this throws any exception provide me the stack trace please
I have a project using Javascript parse json string and put data into div content.
In this case, all of itemname variables is the same div's id.
If variable i about 900, I run this code in Firefox 3 for <10ms, but it run on IE 7 for >9s, IE process this code slower 100 times than Firefox
I don't know what happen with IE ?
If I remove the line document.getElementById(itemname), speed of them seems the same.
The main problem arcording to me is document.getElementById() function?
Could you show me how to solve this prolem to increase this code on IE ?
Thank in advance.
var i = obj.items.length-2;
hnxmessageid = obj.items[i+1].v;
do{
itemname = obj.items[i].n;
itemvalue = obj.items[i].v;
document.getElementByid(itemname);
i--;
}while(i>=0);
Are you really noticing any latency?
gEBI is natively very very fast, I don't think you can avoid it anyway for what you're doing. Could you provide a low-down of what you're doing precisely? It looks like you're using a loop, but can you post exactly what you're doing inside of the loop, what your common goal of the script is?
document.getElementByid(itemname) is the fastest way to get a single element from the DOM in any real application you will sould not see any problems with using it, if you do see a problem you need to rethink you code a little it possible to acomplish any task with just a handfull of calls for this method. You can present you full problem if you like so I could show you an example
At least cache the reference to document:
var doc = document;
for(;;) {
doc.getElementById(..);
}