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 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2008
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Write the contents of a variable into the source

    Dear all,


    Imagine I have the variable myNumber in a javascript script.
    myNumber contains the digit 44 in it. I can print myNumber in many ways (alert, document.write..) but I do not want to do this, I do not want to see 44 in a browser, I need 44 written in the source code so my parser can pick it up.

    The problem: myNumber is a dynamic variable, its value changes, that's why I cannot write it down like this: var myNumber="44";
    The origin or/and the usage of myNumber won't give you any clues, and believe me, it is a loooong story, but I'll be happy to clarify anything else for you to have some inspiration if possible.

    Thanks a ton in advance!

  • #2
    Regular Coder
    Join Date
    Dec 2008
    Location
    Tannhäuser Gate
    Posts
    286
    Thanks
    7
    Thanked 58 Times in 57 Posts
    You don't want to see that var in a browser but you want it to be in your source code?
    What source code? Of your page, of your script?
    Either way, if you want to have a var declared, it is going to be be available to anyone watching your page. If you put it in external script file it is not going to be visible in page's source code, but it can still be accessed just by navigating to that script file.
    Of course you don't have to display it for view in browser window, but it is going to be there.

    Origin and usage may give us some clues, because maybe there are other ways of defining that var, depending on what it represents and what triggers its changes.

    I'm not exactly sure I understand what are you trying to achieve, sorry.

  • #3
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    Would be nice if you could show us exactly where you want said variable to appear. "In the source" is a bit flaky, considering all you need to do to have a variable used by a function, etc... is call said variable. What does your parser do? What makes the variable change? Is it a global variable you're defining when it's being generated? Really, just a description likely won't be enough, some actual code would definitely help with solving the issue.
    Last edited by Eldarrion; 03-12-2009 at 04:23 PM.

  • #4
    New Coder
    Join Date
    Feb 2008
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for answering freedom_razor
    Sorry I didn't explain myself properly. A parser will go through the code looking for the number.. here's a further explanation:

    What I have:
    <html>
    <head>
    <script>
    ..
    var myNumber=anotherVariable;
    ));
    ..
    </script>
    ...

    The parser reads and stores what I have written before, all the characters written. Therefore, it does not store any value, it won't store the value of anotherVariable, it will store --anotherVariable-- the fifteen characters.

    If I had, let's say:

    <html>
    <head>
    <script>
    ..
    var myNumber="44";
    ..
    </script>
    ...
    the parser would read the whole thing again and it would store it. The number 44 (character 4 and character 4) would be stored along with the rest of characters of the page. And bingo! in here I could look for my 44, isolate it, store it in a variable and use it.

    The problem, the reason why I'm bothering you, is that in my code I don't have var myNumber="44"; myNumber equals to anotherVariable that comes from a document called form.html and that is something dynamic (the value changes, now can be 44 later 589 and so on).

    So right now the parser stores anotherVariable (character by character ) not its numeric value. That's why I need to print it, write it in the source (if possible)...don't mind where or if someone can see it, it is not sensitive information.

    Looking for an alternative of getting anotherVariable's value .. .nothing is passed through URL, anotherVariable comes from the value of a hidden field triggered through a method=link in a form in form.html. As I cannot use PHP or any server side language I had no other option than this hidden field..

    Please tell me if this explanation helps and thanks once again for your help

  • #5
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    In this case, it really depends on how exactly said parser is assigning the value to your myNumber variable. Example:

    Code:
    var myNumber = anotherVariable;
    Should produce the value of anotherVariable inside myNumber, as opposed to:

    Code:
    var myNumber = "anotherVariable";
    Which will produce the value of myNumber being a string: "anotherVariable" due to the double quotes around the name of the variable. Other than that, I have no idea why it would behave like that... if anotherVariable wasn't global, you'd get an "undefined" error, rather than what you're experiencing. Try the simplest debug... do an

    Code:
    alert(anotherVariable);
    Right before the point where you're assigning the value of myNumber to see what is stored... if what comes up is what is supposed to (Like the number 44)... there's something wrong in the way you're assigning the variable.
    Last edited by Eldarrion; 03-12-2009 at 05:55 PM. Reason: Added some more clarity

  • #6
    New Coder
    Join Date
    Feb 2008
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Eldarrion (and thanks for your previous post, I sent mine without seeing it)

    Here's form.html
    Code:
    var id ="44"; // this is an example, 44 comes from more action ;)
    <FORM NAME='miform' METHOD='LINK' ACTION='test.html'>
    <input id='page' name='page' type='hidden' value='"+id+"'/>
    </FORM>
    test.html

    Code:
    var anotherVariable = document.miform.page.value;
    myNumber = anotherVariable;
     alert (myNumber) ;
    The alert does not show, an error first: "document.miform has no properties"

    The way I would parse test.html if it had the number written down and not wrapped in a variable would be (using jQuery):

    Code:
    $(document).ready(function(){
    $.get("test.html", function(data){
    var n=data.match(/var myNumber\=\"(\d+)\"/);
    var final=n[1];
    ... a happy human being using variable final which contains number 44 ;)
    });
    });
    Thanks again!

  • #7
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    Hmmm, why would you use it like that though? Why not just stick the JS in a separate .js file... and considering you're using jQuery... why not just have it do...

    Code:
    function setvarmn() {
        var myNumber = $("page").val();
        alert(myNumber);
    }
    Which would in turn return the value, stored in the element with id "page". Seeing how your anotherVariable comes from the same location, really. I'm guessing you have some other JS that assigns the specific value to that element, which runs before your myNumber is used. All in all, I don't see a reason why you'd want to be reading and parsing test.html instead of using JS itself to get the value. Not from the code you provided anyways.


  •  

    Posting Permissions

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