I just started learning javascript and i have a piece of code that is not working and i can't figure out what is wrong.
I have already checked the names of everything if they are spelled correctly multiple times.
function beautify() {
document.write("ja");
var item = document.getElementById("top");
item.classList.add("beautiful");
return;
}
Please refer to https://javascript.info
A very good website to refer.
document.body.innerHTML = "<button onclick='beautify()'>Click Me</button>";
document.body.innerHTML += "<h1 id='top'>Manish</h1>";
function beautify()
{
var item = document.getElementById("top");
item.classList.add("beautiful");
}
.beautiful{
color:red;
}
To debug the code and try to understand the problem, i would use console.log().
function beautify(){
console.log('function is executed');
document.write("ja");
var item = document.getElementById("top");
item.classList.add("beautiful");
return;
}
beautify();
So you execute the function - by calling function_name(); - after you have declared it, and check if the console.log() writes something through your browser console. For example on Firefox or Chrome by pressing F12 you can access it.
EDIT: I forgot to mention you can also see Javascript errors through the console in case
Related
In my code I need to do this:
function myFunction() {
/*more code*/
let id = 12;
let name = "something";
deleteBtn.setAttribute("onclick", "otherFunction("+id+","+name+")");
}
In which
function otherFunction(id, name){
/*does other stuf*/
}
The issue is, when I run the code and load the html page I get this error:
SyntaxError: missing ) after argument list.
I can't figure out what I'm doing wrong.
when it comes to syntax error recheck what you have typed
first i dont see you declare the deleteBtn variable
deleteBtn.setAttribute("onclick", "otherFunction("+id+","+name+")");
check this code it works
document.getElementById('asd').setAttribute('onclick', `otherFunction("${id}","${name}")`);
On one my page a piece of code is showing when I do inspect element and I have not written this code anywhere. It is still showing when I clear the console.
function hack() {
function hacker() {
console.log(myvar);
}
var myvar = 2;
hacker();
}
var myvar = 1;
hack();
hacker();
This is the peice of code which I am getting. Is it related to hacking ? Is someone hacked my site and inserted their code ?
Assume I run my Javascript project in a browser and I'm inside a specific module, can I check whether is already message printed to the console ? i.e. read message from the console...
For example I'm inside my js.file inside function want to check if already printed hello world in the console.
jthanto's answer gave me an idea. I don't think it's good practice, but if you must, you can define your own console class:
var MyConsole = function(oldConsole) {
// store all messages ever logged
this.log = [];
// keep a pointer to oldConsole
this.oldConsole = oldConsole;
}
MyConsole.prototype.log = function(args) {
// push the message into log
this.log.push(Array.prototype.join.call(args));
// call oldConsole.log to actually display the message on the console
if (this.oldConsole)
this.oldConsole.log.apply(this.oldConsole, args);
}
// TODO: implement all other console methods in this fashion (apply for all console API methods)
MyConsole.prototype.<method> = function(args) {
if (this.oldConsole)
this.oldConsole.<method>.apply(this.oldConsole, args);
}
// method to check if something was printed
MyConsole.prototype.wasLogged(message) {
return this.log.indexOf(message)!==-1;
}
// replace console with an instance of MyConsole, pointing to the old console
console = new MyConsole(console);
Save it in a file and load it first (right at the top of your tags)
Use it like:
if (console.wasLogged("Hello World"))
doStuffz();
Hope it helps. Mind it's not tested, but should give you some pointers :)
You could always define your own function for "console.logging" one or more messages (if this is what you are doing), and have a boolean in this function to handle this sort of thing.
I would bet it's not "best practice", but it would solve your problem in some degree.
var messageSent = false;
var myConsoleLog = function($strMessage){
if (!messageSent) {
console.log($strMessage);
messageSent = true;
} else {
// Do whatever you feel like
}
};
Of course if you need to check for more cases you will need to alter the function to actually keep track of more messages. :)
Normally it can't be done. Look at Chrome console's API:
https://developer.chrome.com/devtools/docs/console-api
But this experimental Chrome feature can solve your problem: https://developer.chrome.com/extensions/experimental_devtools_console
Unfortunately it looks like other browsers doesn't have tools like this.
I'm trying to learn basic javascript.
I've created this jsfiddle:
http://jsfiddle.net/Friar_Broccoli/b2gur/
function useless(callback) { return callback(); }
var text = 'Domo arigato!';
assert(useless(function(){ return text; }) === text,
"The useless function works! " + text);
which is straight out of page 37 of:
http://netcraft.co.il/fedia/books/SecretsoftheJavaScriptNinja.pdf
It does NOTHING, and if I add:
document.writeln(text);
it works only if I place it immediately after the "var text = .." declaration. Not the first time I've had this type of problem, although I sometimes succeed in getting javascript functions to work properly.
So
(1) Why does nothing work after the assert() call?
(2) How can I make it work?
(3) Is there somewhere I can find a for-morons explanation of how to organize code in a *.js file?
Thanks;
1) There is no assert() function, so the code fails and doesn't do the rest. If you put an output right after " var text = 'Domo arigato!'; ", it works only because it simply manages to get up to that point without an error.
2) You need to define your own assert function, something like :
function assert(condition,okMessage,failMessage){
if(condition) document.writeln(okMessage);
else document.writeln(failMessage);
}
3) There is no problem with your code organization.
Your fiddle has no defined "assert" method.
This is a good read for you :
http://www.w3schools.com/js/js_functions.asp
functions don't start themself, you need to start them with an onload / onclick / etc event.
I would like to add new attribute to select box which name and id are 'firm_id'. So far I have tried with this code, its working fine in mozila but not working in IE.
I am doing this with javascript because select box is coming from ajax.
The function sbmtfrm() is not calling in IE.
Error: Message: 'FB' is undefined.
May be FB is a object called in my js lib files, but now i am writing code within a another saperate script tag.
<script type="text/javascript">
function sbmtfrm()
{
alert('now submitting...');
document.frmsearch.submit();
}
function setOnclickAtt(name)
{
alert("'"+name+"'" + document.getElementById(name).getAttribute('onchange'));
alert(document.getElementById(name));
if(document.getElementById(name))
{
alert('attrr changed');
var ref = document.getElementById(name);
ref.setAttribute('onchange', 'sbmtfrm();');
alert("now new atrr = " + document.getElementById(name).getAttribute('onchange'));
}
else
{
alert('again');
setTimeout("setOnclickAtt('firm_id')",100);
}
}
setOnclickAtt('firm_id');
</script>
Any suggestion or ideas would be greatly appreciated.
Thanks a lot.
I think IE is picky when it comes to event handling. Try:
ref.onchange = sbmtfrm;
instead of:
ref.setAttribute('onchange', 'sbmtfrm();');
Also, I think the error message has nothing to do with this issue. It´s wrong but it´s another issue.