0

I'm trying to change the text inside a h1 tag using the element id "greeting" and a external java script. I'm having trouble trying to figure out how to call upon my function init that has gathered variables and use them in the next function. Heres what I have so far.

HTML

 <head>

    <title>Matt Beckley</title>
    <script type="text/javascript" src="script1.js"></script>
</head> 

<body>

 <h1 id="greeting">Welcome!</h1>

 </body>
</html>

THE SCRIPT WHERE THE PROBLEM IS!

 window.onload = init;

 function init() 
 {
 var fullname=prompt("Please Enter you name!","Anonymous ");
(fullname == "Anonymous")? "Anonymous " : "";

 var nickname=prompt("Please Enter you nickname!"," None");
 (nickname == "None")? " None": ""; 
 }

 function writeGreeting()
 {
    document.getElementById("greeting").innerHTML= ?????????
 }

So I don't know how to make the last function work so that the h1 tag shows a name and a nick. w3schools only shows "Strings" and not the ability to call the other function. Anyone know how to do this?

6 Answers 6

1

How about merging the init and writeGreeting functions:

window.onload = init;

function init() 
{
 var fullname=prompt("Please Enter you name!","Anonymous ");
 fullname = (fullname == "Anonymous")? "Anonymous " : "";

 var nickname=prompt("Please Enter you nickname!"," None");
 nickname = (nickname == "None")? " None": ""; 
 document.getElementById("greeting").innerHTML= "Welcome " + fullname + " (" + nickname + ")";
}

p.s. Minor point of grammar - you probably want "Please enter your name", rather than "Please Enter you name", and similarly for the nickname prompt.

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

Comments

1

Why not use .inner​Text instead?

2 Comments

Because it's IE-only. On other recent browsers you can use the DOM Level 3 Core property textContent instead. But yes, you definitely don't want to be writing unescaped text to innerHTML.
As above, innerHTML is dangerous: slideshare.net/x00mario/the-innerhtml-apocalypse
0
// Create two GLOBAL variables
var fullname = "";
var nickname = "";

function init() {
  fullname = /* ... */
  nickname = /* ... */
  // pass the nickname on to the greeting function
  writeGreeting(nickname);
}

// This function is remain separate, if you wish to call it again
// at a later time before a page-refresh.
function writeGreeting(name) {
  document.getElementById("greeting").innerHTML = name;
}

Comments

0

How about like this?

It will display "Welcome fullname!" or "Welcome nickname!" or "Welcome Anonymous!" if you don't put any.

function init() {
     var fullname=prompt("Please Enter you name!");
     var nickname=prompt("Please Enter you nickname!");
     writeGreeting(fullname||nickname||"Anonymous");
}

function writeGreeting(name){
    document.getElementById("greeting").innerHTML= "Welcome "+name+"!";
}

Comments

0

I like Dominic's solution. Just bear in mind that you should be careful with any data entered by a user. I would recommend escaping string with escape() function, prior to using it.

e.g.

document.getElementById("greeting").innerHTML= "Welcome " + escape( fullname ) +
                                               " (" + escape( nickname ) + ")";

2 Comments

That's the wrong escape, it's a JavaScript-specific encoding that is a bit like URL-encoding, but broken. It would mangle common name characters like spaces and accents. escape should never be used for anything. The right escape would be HTML-encoding, replacing < with &lt; and & with &amp;.
Ah. Yet, you have to agree that it is still better than nothing. :)
0

Try this. Tested with ie, chrome and ff (all latest).

var msg = "Welcome " + fullname + "(" + nickname + ")!";

var greetingElement = document.getElementById("greeting");

if(greetingElement.firstChild == null)
    greetingElement.appendChild(document.createTextNode(msg));
else
    greetingElement.firstChild.nodeValue = msg;

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.