1

I want to make sure a javascript variable is getting the correct values passed into it when setting it to a ruby variable.

<script type="text/javascript" charset="utf-8">

   var test = <%= @test || 'null' %>;
   document.write(test);

</script>

However, there doesn't seem to be any output whatsoever. I've tried just document.write("somethinganything") and I still don't see any output. Is it supposed to appear on webpage or terminal? I couldn't find any in both.

3
  • If you use the browser's View Page Source option what does that code look like? The @test variable could contain a value that creates invalid JavaScript so then it would stop before it gets to the document.write(). Your document.write("somethinganything") should work, as you can see here: jsfiddle.net/Aa5nn, unless you have it after the same var test = ... line with an error. Commented Dec 2, 2012 at 20:54
  • what do you see if you view source? it is possible that the parser is stopping at the var test ... line due to some syntax error and never makes it to the following document.write line Commented Dec 2, 2012 at 20:54
  • Ended up using console.log, was able to track the variable value using firebug after. Thanks everyone! Commented Dec 2, 2012 at 21:01

3 Answers 3

6

document.write prints to the webpage. You should try using console.log to print to the terminal.

Sign up to request clarification or add additional context in comments.

7 Comments

This doesn't answer the OP's question. He didn't see any output in the document OR the terminal.
The only question in the question itself is "Is it supposed to appear on webpage or terminal? I couldn't find any in both."
@TheSmose You can use document.write() from the head section fyi.
@TheSmose I'm trying it inside notepad++ right now, with an empty body, and it's showing up in the top left hand corner of the page..
I apologize TheSmose. It was my fault for not explicitly stating what I really wanted to figure out. Although I was questioning why I couldn't see the output, my main concern was trying to find a way to see what the value of the variable was being set as. I just ended up giving the checkmark to whoever gave the answer first. I'll make sure to write down exactly what im looking for next time. Sorry about that.
|
3

You shouldn't use document.write() to print out visible content unless the script block is in the <body>.

Some older browsers will not support that, and if the script block is in the <head> it will not write anything visible.

Try one of the following solutions instead:

// Append a text node to the end of your body
window.onload = function() {
    var txt = document.createTextNode(test);
    document.body.appendChild(txt); 
};

or

// Log the text to your javascript console
console.log(test);

or

// Alert the text in a popup
window.alert(test);

2 Comments

document.write() works from script blocks in the <head> - do you have some supporting documentation for why you think it doesn't?
@nnnnnn I stand corrected. Updated my answer to correct my statement.
1

you have to write console.log("test");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.