Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Weird string lengths issue

    Hi, this one is driving me nuts, hopefully there is an easy answer.

    agent = '@Agent.user_name@';
    validAgent = "mark e-mail address";

    document.write("<p>'" + agent.length + "," + validAgent.length + "'</p>");
    document.write("<p>'" + agent + "', '" + validAgent + "'</p>");

    this prints,

    17, 19
    "mark e-mail address", "mark e-mail address"

    so the first string length appears to be wrong?

    also,

    if(agent == validAgent) <-- this fails


    So, I can see that agent is being set to "mark e-mail address", because I print it out, but it is like the code is still seeing agent as '@Agent.user_name@'.

    I hope this is clear?

    Any thoughts?

    many thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    I don't understand your problem. When I run the code I get

    '17,19' (in quotes as specified)

    '@Agent.user_name@', 'mark e-mail address'

    Which is exactly what I would expect. Why do you think that the variable agent is being set to the value "mark e-mail address",

    if(agent == validAgent) <-- this fails. Well, of course it does. The two strings are not the same.


    Note that document.write is obsolete except for testing purposes.

    Quizmaster: The Holy Roman Emperor Charles the First is better known by what one-word name?
    Contestant: Charlie
    Last edited by Philip M; 05-11-2013 at 01:39 PM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    weird string lengths

    Sorry, I should have been clearer.

    If you look at what the document write is outputting, you can see that @Agent.user_name@ is set to 'mark e-mail address'. How, I am not sure.

    The debug shows that when @Agent.user_name@ is printed, it is 'mark e-mail address', but the length is wrong.


    Quote Originally Posted by Philip M View Post
    I don't understand your problem. When I run the code I get

    '17,19' (in quotes as specified)

    '@Agent.user_name@', 'mark e-mail address'

    Which is exactly what I would expect. Why do you think that the variable agent is being set to the value "mark e-mail address",

    if(agent == validAgent) <-- this fails. Well, of course it does. The two strings are not the same.


    Note that document.write is obsolete except for testing purposes.

    Quizmaster: The Holy Roman Emperor Charles the First is better known by what one-word name?
    Contestant: Charlie

  • #4
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    No, it behaves exactly as you would expect it to: http://jsbin.com/oyohav/1/edit
    If I had to guess: Your actual code does more than just the few lines you provided. Either share the entire code or provide an example that shows the behavior you're describing.

  • #5
    New to the CF scene
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry if I have not made it clear...

    the point is that @Agent.user_name@ is some sort of macro. While is appears as @Agent.user_name@ in the code sample, when it is used in a document write, it outputs "mark e-mail address".

    I am not sure how the macro works, I just know that in the system I am using, I can use these variables.

    I am after some pointers as to why document.write outputs the subsituted macro, while the string length is that of the macro itself.

    Appologies if my terminology is not 100% correct.

    Please look at the output shown in my first post.

    Many thanks for your help!

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    I don't see how we can help you unless you post all of the (relevant) code. There is no such thing as a macro in Javascript. Are you using some sort of code generator like Dreamweaver?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Well, the fact you are using document.write makes the whole thing suspicious. I'm sure it must be Felgall going around behind your back and changing your code to get rid of such an obsolete construction! <grin/>

    SERIOUSLY...

    We can't possibly guess what your "macro" is doing if you don't tell us what technology you are using for your macros. I've never encountered a system that use @..@ to indicate value substitution. (I think it's a poor choice of delimiting characters, since you could easily find @..@ in ordinary test, say with a pair of nearby email addresses.) PHP use <?..?>, ASP and JSP use <$...$>, etc. But all of those are server-side coding, of course. You seem to be saying you have some client-side system doing this.

    Okay, I can believe that. But what system?

    Can you possibly show this on a live web page? Give us the URL?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New to the CF scene
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Unfortunately it is part of a call centre application, so there is no way I have of showing it running. Even worse, there is such little documentation, and maybe I am trying to do something that was never intended.

    I have a html file with javascript, and as part of the call centre application, I can use various variables such as @customer.number@ (variables are always in @@).

    These variables are displayed in the custom browser ok, and are output correctly with a document write, but as for the rest of it, the code appears to be working on the variable name rather than the variable value.

    I know this is a custom case that nobody else is going to be able to reproduce, but I was just looking for pointers! thank you

  • #9
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Well there is probably some kind of script that will go through the page after it finished rendering and replace the macros -- but during the runtime of your script, the string is just still the one containing the macro name, not its value.

    Without knowing how the system works, I don't see how we can help you solve it, though. Sorry.

  • #10
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,774
    Thanks
    55
    Thanked 518 Times in 515 Posts
    if @Agent.user_name@ is in fact a variable I would suspect that this:
    Code:
    agent = '@Agent.user_name@';
    is one of your problems - wrapping a variable in quotes will make it a string. Without knowing anything else, I would suggest that
    Code:
    agent = @Agent.user_name@;
    would be a better approach

    as to why your two variables end up with the same value, nearly impossible to say without seeing more code, although my suspicion is that somewhere along the line when you say you are doing this:
    Code:
    if(agent == validAgent)
    you are in fact doing this:
    Code:
    if(agent = validAgent)
    but that's all guesswork, which is pretty much the best you can hope for, with the information supplied.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •