View Full Version : Write the contents of a variable into the source

03-12-2009, 11:13 AM
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!

03-12-2009, 01:46 PM
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.

03-12-2009, 03:20 PM
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.

03-12-2009, 04:27 PM
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:
var myNumber=anotherVariable;

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:

var myNumber="44";
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

03-12-2009, 04:47 PM
In this case, it really depends on how exactly said parser is assigning the value to your myNumber variable. Example:

var myNumber = anotherVariable;

Should produce the value of anotherVariable inside myNumber, as opposed to:

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


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.

03-12-2009, 05:19 PM
Thanks Eldarrion (and thanks for your previous post, I sent mine without seeing it)

Here's form.html

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+"'/>


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):

$.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!

03-12-2009, 06:26 PM
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...

function setvarmn() {
var myNumber = $("page").val();

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.