8 Replies - 448 Views - Last Post: 18 June 2012 - 08:03 AM

#1 synlight  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 259
  • Joined: 14-September 11

Super simple script, doc write not rendering

Posted 18 June 2012 - 07:25 AM

I'm in my first Javascript class. I've been staring at this assignment for about an hour, trying to figure out why my document.write() lines won't render in my browser. The prompt boxes come up just fine, and everything else in the body tags shows up. I know I'm missing something simple. I've been through my textbook, the prefessor's powerpoints, and a few online references, and it looks like i have my syntax correct. What am I missing? Thank you!


<html>
<head>
<title>Chapter 3 Assignment 2</title>
<script type="text/javascript">

var first_name = prompt("What is your first name?","");
var last_name = prompt("What is your last name?","");
var hourly_wage = prompt("What is your hourly wage?","");
hourly_wage = parseFloat(hourly_wage)
var weekly_pay = hourly_wage*40;
var job_id;
var job_title;
</script>
</head>

<body bgcolor="lightblue">
<script type="text/javascript">
job_id = "B0123456";
job_title = "Programmer";
document.write(<h1><b>"First Name: "</b> + first_name);
document.write(<h1><b>"Last Name: "</b> + last_name);
document.write(<h1><b>"Job Title: "</b> + job_title);
document.write(<h1><b>"Job ID:< "/b> + job_id);
document.write(<h1><b>"Weekly Pay: $"</b> + weekly_pay);
</script>
<a href="http://www.tridenttech.edu/">Trident Technical College</a> 
<h1><font color = "yellow">THE END</font></h1>
</body>
</html>


Just noticed this:

document.write(<h1><b>"Job ID:< "/b> + job_id);


Is This A Good Question/Topic? 0
  • +

Replies To: Super simple script, doc write not rendering

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2888
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Super simple script, doc write not rendering

Posted 18 June 2012 - 07:27 AM

check the Error Console, document.write() expects a string as argument.

note: document.write() will kill your page if used after the page finished loading.

hint: our syntax highlighter displays strings in blue.

This post has been edited by Dormilich: 18 June 2012 - 07:29 AM

Was This Post Helpful? 1
  • +
  • -

#3 synlight  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 259
  • Joined: 14-September 11

Re: Super simple script, doc write not rendering

Posted 18 June 2012 - 07:31 AM

Didn't even know the error console existed! Thanks!!!

So, it should show my variables in blue if they are strings, or just the literal strings?

This post has been edited by synlight: 18 June 2012 - 07:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2888
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Super simple script, doc write not rendering

Posted 18 June 2012 - 07:36 AM

the literals. since JS is weakly typed, you would need a JS interpreter to know, which variable contains what.
Was This Post Helpful? 1
  • +
  • -

#5 synlight  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 259
  • Joined: 14-September 11

Re: Super simple script, doc write not rendering

Posted 18 June 2012 - 07:37 AM

hmm. okay, took out the h1 and bold tags and it works now. Still have to format a bit, but at least it's rendering.

I have another question though.

The example i am supposed to follow shows this:

document.write("<h1><b>String+variable</h1></b>


Will that even work, putting the format tags inside the string? I tried it that way and it wouldn't render.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2888
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Super simple script, doc write not rendering

Posted 18 June 2012 - 07:38 AM

of course it will not work, that’s an incomplete call (closing parenthesis of the function is missing)
Was This Post Helpful? 0
  • +
  • -

#7 synlight  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 259
  • Joined: 14-September 11

Re: Super simple script, doc write not rendering

Posted 18 June 2012 - 07:41 AM

Oops. That was my bad, I forgot to type it in. To rephrase my question: will putting the format tags inside a string work? It seems like it would just print the tags themselves, instead of formatting the output.

Bah. nevermind. It works. I just don't understand why, if you don't mind explaining it to me. I'd like to understand why it works instead of just typing it in and expecting it to magically work.

This post has been edited by synlight: 18 June 2012 - 07:43 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2888
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Super simple script, doc write not rendering

Posted 18 June 2012 - 07:57 AM

the point is, you dump a HTML string into the parsing document stream (that’s why it only works while loading). and then it is treated as if it would have been in the source code. it is rendered as HTML code.
Was This Post Helpful? 0
  • +
  • -

#9 synlight  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 259
  • Joined: 14-September 11

Re: Super simple script, doc write not rendering

Posted 18 June 2012 - 08:03 AM

View PostDormilich, on 18 June 2012 - 09:57 AM, said:

the point is, you dump a HTML string into the parsing document stream (that’s why it only works while loading). and then it is treated as if it would have been in the source code. it is rendered as HTML code.


Ahhh thank you!! My assignment is running perfectly now. Appreciate the help, and the explanation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1