-
Jan 25th, 2022, 09:35 AM
#1
[RESOLVED] Wierd issue in javascript
I have ran into an issue this morning that has me a bit dumbfounded.
below is a portion of the line that is causing my headaches
Code:
<td><button class='smallbutton' onclick='ReleaseStation("+station+");'>"+istatus+station+"</button></td>
There are several variables available here one of which is named clid
The ReleaseStation function expects the clid to be passed but for whatever reason when I write it this way nothing happens
Code:
<td><button class='smallbutton' onclick='ReleaseStation("+clid+");'>"+istatus+station+"</button></td>
the clid occurs again in another column and is displayed as expected.
I have also tried replacing the call to ReleaseStation with just an alertbox and get the same result which is if I have
alert("Test"); it works
alert("+station+"); it works
alert("+clid+"); nothing happens.
I have no idea what is going on here. To me that should be working but it is not.
Any ideas?
-
Jan 25th, 2022, 10:03 AM
#2
Re: Wierd issue in javascript
btw both station and clid are strings and both contain a value
Also if I place the literal value of clid in there it works but fails to execute the function when the var clid is used????
-
Jan 25th, 2022, 03:01 PM
#3
Re: Wierd issue in javascript
What's the value of "clid"? Could there be any characters (like ', ") that need escaping?
-
Jan 25th, 2022, 03:14 PM
#4
Re: Wierd issue in javascript
here is an example of the content in the clid var
0121_0356_14X72_2
If I use the literal value it works but when using the variable that holds this value it does not appear to call the function at all.
-
Jan 25th, 2022, 03:30 PM
#5
Re: Wierd issue in javascript
Hmm, nothing weird about that content that I can see.
If you go to the Console page in your browser's Developer's Tools, what errors (if any) appear when you click the button?
-
Jan 25th, 2022, 03:31 PM
#6
Re: Wierd issue in javascript
Also, I'm assuming that the HTML snippets you posted are actually inside JS strings, and not raw HTML? Because if that is raw HTML then your "+clid+" is inside a string and won't be evaluated as a variable.
-
Jan 25th, 2022, 03:36 PM
#7
Re: Wierd issue in javascript
I'd change this:
Code:
<td><button class='smallbutton' onclick='ReleaseStation("+clid+");'>"+istatus+station+"</button></td>
To this:
Code:
<td><button class='smallbutton' onclick='ReleaseStation("+clid+");'>"+clid+"</button></td>
And display the clid value in the button, just to make sure it is what you think it is at the time it should be ...
If it's working with station, there's no reason to think it wouldn't work with clid, unless clid isn't what you think it is.
-tg
-
Jan 25th, 2022, 04:17 PM
#8
Re: Wierd issue in javascript
Yes that is within a larger line of javascript. as written in the first snippet it works as expected, the second one does not. The only difference is the variable in use.
The clid is written in the next column of the table. normally it is hidden but I did make it visible just to be sure it was what I thought. It looks exactly as expected. I copied the example value above directly from the table. I looked at the array that comes from the server which is where the clid is generated and there too it looks exactly as expected. I even tried pasting in the literal value and that worked but using the variable has failed everytime.
I'll take another look at the console display and see what I can find there.
-
Jan 25th, 2022, 04:45 PM
#9
Re: Wierd issue in javascript
Well, as a last ditch effort I tried using quotes around the variable and it looks as though it might have worked. No error and the value of clid is carried into the function. I will have to go a little deeper, restore the function and see if it executes properly.
-
Jan 25th, 2022, 04:57 PM
#10
Re: Wierd issue in javascript
So it looks like I got it working though am still confused as to why this was needed.
Code:
<td><button class='smallbutton' onclick=\"ReleaseStation('"+clid+"');\">"+istatus+station+"</button></td>
I guess it was work with station because even though it was a string it contain a numeric value and clid was alpha numeric.
I do not do much browser based stuff and had not thought to look under the developers tools for some kind of error message doing that helped me track it down.
-
Jan 25th, 2022, 05:00 PM
#11
Re: [RESOLVED] Wierd issue in javascript
Maybe because the clid started with a number? Not sure if JS will try to automatically convert it to a number unless you force it to be a string?
-
Jan 25th, 2022, 05:01 PM
#12
Re: [RESOLVED] Wierd issue in javascript
A yes, here's the error in the console:
Code:
alert(0121_0356_14X72_2);
Uncaught SyntaxError: numeric separators '_' are not allowed in numbers that start with '0'
-
Jan 25th, 2022, 05:13 PM
#13
Re: [RESOLVED] Wierd issue in javascript
Apparently JS allows underscore as a separator for numeric literals to make them easier to read in code. e.g.: 10000000 can be written as 10_000_000
Guess the lessons learned here are:
1) Always use quotes around variables that you want to use as text - don't rely on JS to convert them to strings for you.
2) Check your console when things aren't working as expected - error messages appear there that can point you in the right direction.
-
Jan 25th, 2022, 05:57 PM
#14
Re: [RESOLVED] Wierd issue in javascript
The odd thing is that the clid was sent down as a string, showed in the console on the return from the server as a string and in some cases had alpha characters in it but still failed unless the quotes were added around it for the function call. Kind of like when you are passing a string variable into sql query. I feel a bit stupid that it took me that long to find it but I really never would have guessed that would be the issue here.
One thing for sure, I'll remember this, the console and will be more careful next time.
-
Jan 25th, 2022, 09:41 PM
#15
Re: [RESOLVED] Wierd issue in javascript
It's the difference between this:
and this:
Code:
alert('this is a string');
This will break:
Code:
alert(This is a string);
which is essentially what you were trying to do ...
This is one of the things I hate about JS... it's loosey goosey treating with types ...
-tg
-
Jan 25th, 2022, 11:49 PM
#16
Re: [RESOLVED] Wierd issue in javascript
yep apparently it was not treating the string the way I expected it to
Var mystring="this is a test";
alert(myString);
You would expect it to be treated as
alert("this is a test");
but apparently it was seeing it as
alert(this is a test);
and failing
Whats worse is in some cases it was working so yeah. I definitely do not like that aspect of it.
-
Jan 26th, 2022, 01:19 AM
#17
Re: [RESOLVED] Wierd issue in javascript
but you didn't have alert(myString) ... you had alert("+clid+") ... the clid isn't part of the javascript ... it's value is ... so you need to enclose its VALUE in quotes to have it treated like a string.
-tg
-
Jan 26th, 2022, 07:08 AM
#18
Re: [RESOLVED] Wierd issue in javascript
I do hate those difficult issues that arise when you are fighting against javascript's loose typing. Thumbs up to the solution JB.
If .js has ONE flaw then it is inadequate typing.
https://github.com/yereverluvinunclebert
Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.
By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.
-
Jan 26th, 2022, 09:58 AM
#19
Re: [RESOLVED] Wierd issue in javascript
Originally Posted by yereverluvinuncleber
I do hate those difficult issues that arise when you are fighting against javascript's loose typing. Thumbs up to the solution JB.
If .js has ONE flaw then it is inadequate typing.
This is off topic, but typescript is basically ES6 JavaScript with typing.
-
Jan 26th, 2022, 11:45 AM
#20
Re: [RESOLVED] Wierd issue in javascript
I'm just glad I managed to find the solution. Such a silly mistake that had me spinning my wheels.
Thanks to jbpro for pointing out the console in developer tools. I had been testing in the default ie and not seeing any messages.
Switched to chrome and opened the console, not only helped with that issue but any that followed.
I will definitely be doing it that way from now on.
In this project I have been working on a remote system and all the coding has been free hand in notepad++
Coding in a text editor really makes you appreciate the power of a good IDE.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|