can't find javascript error - javascript

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.

Related

Javascript doesn't add the class

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

Retrieving number of pages of a PDF using PDF.JS fails with return

I don't understand the following. Maybe I am missing some really stupid issue here... can anybody solve it? Thanks in advance.
I have this code in Javascript:
function funcLeNrPaginasPDF(strCaminhoPDF) {
var objLivrariaPDF = window['pdfjs-dist/build/pdf'];
var objDocumentoPDF = null;
objLivrariaPDF.workerSrc = '/pdfjs/pdf.worker.js';
// strCaminhoPDF var contains the pdf doc: "documento.pdf"
var oprCarregaPDF = objLivrariaPDF.getDocument(strCaminhoPDF);
oprCarregaPDF.promise.then(function(objDocumentoPDF) { document.getElementById('nr_paginas').textContent = objDocumentoPDF.numPages; });
It works like a charm, thus, if I change document.getElementById... and put
alert (objDocumentoPDF.numPages);
it works as well... However if I place a "return" the function returns me an undefined variable content. Like this:
oprCarregaPDF.promise.then(function(objDocumentoPDF) { return objDocumentoPDF.numPages; });
Any thoughts why? I need a variable to hold the number of pages.
Regards.
Solved it. Ashamed of such unimportant and easily spot-table fault. The return must've been put outside the bracket of the inner function... ahmen.

$mdDialog.prompt throws exception, but the function works fine with confirm

For some reason, my prompt dialog stopped working in my yo angular fullstack application.
I googled a solution telling me to update my angular, which i did, but it did not solve the problem.
$scope.showPrompt = function(ev, ret, value) {
var confirm = $mdDialog.prompt()
.title('Rediger ' + value)
.textContent('Indtast en ny værdi for: ' + value)
.placeholder('getValue()')
.ariaLabel('Ny ' + value)
.targetEvent(ev)
.ok('Accepter')
.cancel('Annuller');
$mdDialog.show(confirm).then(function(result) {
//setValue(result);
});
};
Whenever i call the function, i get an error saying TypeError: $mdDialog.prompt is not a function.
If i change the dialog to a .confirm and remove the placeholder it works fine
I realize an answer has already been accepted, but it didn't solve my problem so I wanted to provide an alternate suggestion.
I ran into the exact same situation and it turned out that I was using an old version of Angular Material that hadn't introduced .prompt() yet. If you check out the docs for the version I was using (1.0.5) you'll see in the demo that there is no prompt option for dialog. But if you look at the docs for the latest version (1.1.0 as of this writing), the prompt option is there.
Hope this helps anybody who comes across this question like I did.
Edit:
In javascript a variable as function is defined at runtime :
//This code throw an error
getValue();
var getValue = function(){};
You have to declare your variable function before calling it :
//Ok
var getValue = function(){};
getValue();
You could also write something like
//Ok because code block is parsed before runtime
getValue();
function getValue(){};
So here your plunker edited
https://plnkr.co/edit/YqeyaLqW2B6xn4VHcVlQ?p=preview

Have I done something wrong, or is this a bug? (TypeScript/Visual Studio 2012)

Resolved:
Thank you all for your help. I'm not sure exactly what caused this, but I restarted Visual Studio and everything went back to normal. Not sure why, but it's been working ever since (yesterday).
I didn't have these problems last night (with the same code - unchanged):
I don't see what the issue is.
The error I am getting is:
JavaScript critical error at line 1, column 9 in [path/app.ts]
SCRIPT1004: Expected ';'.
What the deuce?!
Incase you can't see the image, the error is referring to this line:
declare var document;
Update
The javascript file which is a result of the TypeScript being compiled into JavaScript looks like this:
window.onload = function () {
start();
};
function sayHello(msg) {
return msg = "Hello, therel ol!";
}
function start() {
var element = document.getElementById("link");
element.addEventListener("click", function () {
var element = document.getElementById("response").innerText = sayHello("Hi");
}, false);
if(XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
And as you can see, everything looks fine. I don't get why it's throwing this error.
My guess is that you have referenced the app.ts file on your page by mistake, when you should have referenced the app.js file.
I'm assuming you get this error when running your application, not at design time.
i.e.
<script src="app.ts"></script>
Should be
<script src="app.js"></script>
You should not need to declare document - it should already be declared. It comes from the virtual lib.d.ts file which is referenced by default.
Try commenting out the declare line.

Function not defined javascript

For some reason my javascript code is messed up. When run through firebug, I get the error proceedToSecond not defined, but it is defined!
JavaScript:
<script type = "text/javascript">
function proceedToSecond () {
document.getElementById("div1").style.visibility="hidden";
document.getElementById("div2").style.visibility="visible";
}
function reset_Form() {
document.personalInfo.reset();
}
function showList() {
alert("hey");
if (document.getElementsById("favSports").style.visibility=="hidden") {
document.getElementsById("favSports").style.visibility="visible");
}
}
//function showList2() {
//}
</script>
HTML:
<body>
<!--various code -->
<input type="button" onClick="proceedToSecond()" value="Proceed to second form"/>
</body>
The actual problem is with your
showList function.
There is an extra ')' after 'visible'.
Remove that and it will work fine.
function showList()
{
if (document.getElementById("favSports").style.visibility == "hidden")
{
// document.getElementById("favSports").style.visibility = "visible");
// your code
document.getElementById("favSports").style.visibility = "visible";
// corrected code
}
}
There are a couple of things to check:
In FireBug, see if there are any loading errors that would indicate that your script is badly formatted and the functions do not get registered.
You can also try typing "proceedToSecond" into the FireBug console to see if the function gets defined
One thing you may try is removing the space around the #type attribute to the script tag: it should be <script type="text/javascript"> instead of <script type = "text/javascript">
I just went through the same problem. And found out once you have a syntax or any type of error in you javascript, the whole file don't get loaded so you cannot use any of the other functions at all.
important: in this kind of error you should look for simple mistakes in most cases
besides syntax error, I should say once I had same problem and it was because of bad name I have chosen for function. I have never searched for the reason but I remember that I copied another function and change it to use. I add "1" after the name to changed the function name and I got this error.

Categories