Results 1 to 20 of 20

Thread: [RESOLVED] Wierd issue in javascript

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Resolved [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?

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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????

  3. #3
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    Re: Wierd issue in javascript

    What's the value of "clid"? Could there be any characters (like ', ") that need escaping?

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  5. #5
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    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?

  6. #6
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    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.

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  11. #11
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    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?

  12. #12
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    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'

  13. #13
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    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.

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  15. #15
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: [RESOLVED] Wierd issue in javascript

    It's the difference between this:
    Code:
    alert(100);l
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  17. #17
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  18. #18
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    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.

  19. #19
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: [RESOLVED] Wierd issue in javascript

    Quote Originally Posted by yereverluvinuncleber View Post
    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  20. #20

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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
  •  



Click Here to Expand Forum to Full Width