Why is the "Add Note" button giving an exception and printing null on the console? - html

I am trying to add a note using Add Note Button but it is throwing an exception. I handled the error using try...catch block but the note written in Add Note Button is not added and printing null on console.
JSBin for my project:

This piece:
if(notes =='')
{
notesObj = [];
}
else{
notesObj = JSON.parse(notes);
}
is the problem. If there's nothing in the local storage, notes will be null, which means notesObj == '' will be false, and the code will break. You need to change the test so it detects null (and other falsy values) as such:
if(!notes)
{
notesObj = [];
}
else{
notesObj = JSON.parse(notes);
}
There's another problem. This line:
localStorage.setItem('notes', JSON.stringify(notes));
makes no sense. You're setting the item notes in the local storage as the converted json of the notes variable, which is already a JSON string, so it'll fail horribly. What you really want to do is stringify the notesObj variable, which is the array with the actual notes. Change that line to:
localStorage.setItem('notes', JSON.stringify(notesObj));
And that's it. Make those changes and your code will work.

Related

how to position elements?

How to make this following image and paragraph tag come next to each other (image and p tag in left and right respectively) like inline block elements.
I used the span tag bec its inline but i still couldnt figure it out
The response object is an object that contains all the methods for manipulating the outgoing response and for queuing up data that will be part of that response (when it is finally sent). The specific method you asked about:
res.setHeader(name, value)
is one such method for preparing the outgoing response and is documented here. It allows you to configure a header on that response. It will store that header inside the res object and then when the response is finally sent out over the network, this header item will be streamed as part of the outgoing http headers.
The Express library adds a different variation of this method with:
res.set(field, value)
or
res.header(field, value)
These are both identical in the code.
Internally, both of these just add a little bit of extra processing before eventually calling the underlying res.setHeader() from the regular http library. You can use any one of them. The Express version allows you to call res.set(obj) where obj is a set of key/value pairs that are turned into headers.
You can see the code for Express' res.set() here and see how it eventually calls the underlying res.setHeader().
res.set =
res.header = function header(field, val) {
if (arguments.length === 2) {
var value = Array.isArray(val)
? val.map(String)
: String(val);
// add charset to content-type
if (field.toLowerCase() === 'content-type') {
if (Array.isArray(value)) {
throw new TypeError('Content-Type cannot be set to an Array');
}
if (!charsetRegExp.test(value)) {
var charset = mime.charsets.lookup(value.split(';')[0]);
if (charset) value += '; charset=' + charset.toLowerCase();
}
}
this.setHeader(field, value);
} else {
for (var key in field) {
this.set(key, field[key]);
}
}
return this;
};

onClick communication between content and background scripts not working

I am making an application that highlights key words in the current page after the user clicks my icon. I am trying to communicate between my content scripts and background script. However,my code is not working. Does anyone know how it should be written?
Here is my content script:
chrome.extension.onRequest.addListener(function(active,sender,sendResponse){
if(active.length>0){
jQuery(document).ready(function($) {
//rest of word highlighting code
}
})
here is my background.js :
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.extension.sendRequest(active);
});
Do not use the deprecated chrome.extension.sendRequest and matching events. They are old, broken and not supported, which is quite clearly said in the documentation - which shows that you did not go and read it.
The correct ones to use are chrome.runtime.sendMessage and .onMessage, but otherwise the signature is the same.
Well.. Why did you expect that to work? (unless you're not really showing us all relevant code, which is.. not helpful)
chrome.browserAction.onClicked.addListener(function(tab) {
// There is no "active" in the code anywhere to this point.
// It is treated like a variable name, one that was not yet used,
// so its contents are "undefined", and that's what you're sending.
chrome.runtime.sendMessage(active);
// Equivalent code: chrome.runtime.sendMessage(undefined);
});
And on the receiving side:
chrome.runtime.onMessage.addListener(function(active,sender,sendResponse){
// So, here "active" is undefined. It does not have a length
// parameter, and as such causes a fatal exception
// "Cannot read property 'length' of undefined"
// that you might have seen in the console of the page
if(active.length>0){
/* something */
}
})
Whatever you send is usually, but not always, an object (well, it must be JSON-serializable). If you just want to trigger something and not pass any data, there are 2 often-used conventions, either is fine:
Pass command as a value.
// Sender
chrome.runtime.sendMessage({action: "active"});
// Receiver
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if(message.command == "active"){
/* something */
}
// or, useful if you have many different commands:
switch(message.command){
case "active":
/* something */
break;
}
});
Set a boolean in the message:
// Sender
chrome.runtime.sendMessage({active: true});
// Receiver
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if(message.active){
/* something */
}
});

Show different default page depending on stored value

I am trying to make an windows store app where the default page (first page that comes up when app loads) changes depending on stored value.
I have following files
- js
|- default.js
- default.html
- page_A.html
- page_B.html
default.js has the following code:
if (localStorage["value"] == undefined || localStorage["value"] == "pageA") {
localStorage["value"] = "pageA";
//WinJS.Navigation.navigate("page_A.html");
window.location.assign = "page_A.html";
} else {
localStorage["value"] = "pageB";
//WinJS.Navigation.navigate("page_B.html");
window.location.assign = "page_B.html";
}
WinJS.Navigation code does not work at all. So I tried using window.location and what's happening is instead of loading the actual page, it loads an empty page as shown below.
I tried using both href and assign for windows.location object. What's interesting is that it seems like href and assign loads the page because if I have page_A/B.js associated with pageA/B.html and have a simple console.log statement, then the log statement does get logged, but it does not render the page.
Any ideas? I've been stuck for a while.
Try putting your default.js at the root of your project, next to page_A.html and page_B.html, or, and I don't know if this works, you can try calling those pages with ..\page_X.html.
Also, you can add an error handler function to your navigate in case there's something else going on that you're not seeing.
WinJS.Navigation.navigate('page_A.html', {}).then(function () {
// it worked!
}, function (err){
// something went wrong
});

Cannot find method moneyToMicros((class))

I'm trying to programmatically change the max cpc with an AdWords script, but I'm getting an error. The console just says "Cannot find method moneyToMicros((class))". I cannot find any documentation about this error or any other posts about this anywhere. Was wondering if anyone knew how to get around this. Here's a small snippet of the code where the error occurs (error occurs on the line where setKeywordMaxCpc() is called):
while (adGroupIterator.hasNext())
{
var adGroup = adGroupIterator.next();
var adGroupName = adGroup.getName();
if (adGroupRegex.test(adGroupName))
{
if (adGroup.isPaused())
{
adGroup.enable();
adGroup.setKeywordMaxCpc(bidModifier);
}
}
else
{
adGroup.pause();
}
}
I had the same problem and I've just resolved it!
I was passing the value "null" to the function .setKeywordMaxCpc();
So I think you need to check if the variable bidModifier is null before you execute the function .setKeywordMaxCpc(bidModifier );
In my case I was using the value keyword.getFirstPageCpc() to set my bid, and in some words that value is null.

Windows Phone speech recognition - words that are not in grammar

consider a grammar like this ; speech.Recognizer.Grammars.AddGrammarFromList("answer",new string[] { "Go.","no" });
When I say something else that are not in grammar, she says "sorry didnt catch" and then tries to start it again. Same goes for null input.
What I want is that it should only recognize the words in grammar and for everything else it should just pass the recognition. I don't want to see anything like "sorry didnt catch" and second time recognotion. Any idea ? thanks.
Edit : with try-catch I can avoid from second time recognotion if the word is unknown but now it's waiting too long on "sorry didnt catch" part.
try
{
SpeechRecognizerUI speech = new SpeechRecognizerUI();
speech.Settings.ReadoutEnabled = false;
speech.Settings.ShowConfirmation = false;
speech.Recognizer.Settings.InitialSilenceTimeout = System.TimeSpan.FromSeconds(0.8);
speech.Recognizer.Grammars.AddGrammarFromList("answer", new string[] { "Go.", "no" });
SpeechRecognitionUIResult result = await speech.RecognizeWithUIAsync();
if (result.RecognitionResult.Text == "Go.") { .... }
}
catch
{
..... }
In my opinion, you must build your own UI to avoid this. So you should use SpeechRecognizer and then you can handle the input as you want.
In my case I even created two SpeechRecognizer, on with own Wordlist, the other one with default dictionary. It works like a charm, but I couldn't get it to work with SpeechRecognizerUI.