473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help displaying things

Okay, I'm still really new to javascript, so I'm looking for a little help. I
want to grab a name with I'm trying to display a hyperlink in my body, but one of
my functions overwrites it. The code is below. What am I doing wrong?

Thanks!
David

<html>
<head>
<title>
</title>
<script language="javascript" type="text/javascript">
var userName = "";
var randomMessage = "";
function loadPage()
{
userName = prompt("What is your name?", "Please enter your name");
if ((userName == "Please enter your name") || (userName == null))
{
userName = "Anonymous User";
}
document.writeln('Welcome to my web page, ' + userName);
}

function mouseIsOver(site)
{
mouseIsOverMessage = "This link will take you to www." + site;
window.status = mouseIsOverMessage;
}

function mouseIsOut(site)
{
mouseIsOutMessage = "Are you sure you don't want to give www." +
site + " a shot?";
window.status = mouseIsOutMessage;
}

function mouseHasClicked(site)
{
randomNumber = Math.floor(Math.random()*5);
switch (randomNumber)
{
case "0":
randomMessage = "Hang onto your butt, we're heading
to www." + site + "!";
break;
case "1":
randomMessage = "Destination: www." + site;
break;
case "2":
randomMessage = "www." + site + "? Energize!";
break;
case "3":
randomMessage = "Are you ready to visit www." + site
+ "? Here we go!";
break;
case "4":
randomMessage = "So, you think www." + site + " is
better?! Take a look";
break;
}
alert(randomMessage);
}
</script>
</title>
</head>
<body onLoad="loadPage();">
<h1 align="center">
Here are a few of my favorite links
</h1>
<hr />
<a href="http://www.slashdot.org" onMouseOver="mouseIsOver
('slashdot.org');" onMouseOut="mouseIsOut('slashdot.org');"
onClick="mouseHasClicked('slashdot.org');">
Slashdot
</a>
</body>
</html>
--
--------------------------------- --- -- -
Posted with NewsLeecher v3.9 Beta 1
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

May 24 '07 #1
4 1363
Lee
merlin_at@merlinsrealm_dot_net said:
>
Okay, I'm still really new to javascript, so I'm looking for a little help. I
want to grab a name with I'm trying to display a hyperlink in my body, but one
of
my functions overwrites it. The code is below. What am I doing wrong?

Thanks!
David

<html>
<head>
<title>
</title>
<script language="javascript" type="text/javascript">
var userName = "";
var randomMessage = "";
function loadPage()
{
userName = prompt("What is your name?", "Please enter your name");
if ((userName == "Please enter your name") || (userName == null))
{
userName = "Anonymous User";
}
document.writeln('Welcome to my web page, ' + userName);
}
1. You didn't explain clearly what it is that you're trying to do.
2. You included far too much whitespace in the code you posted.
As you can see, this makes it wrap and become difficult to read.
3. You used document.writeln() in a page that has already been
rendered. This clears the page and writes new content.
--

May 24 '07 #2
On May 24, 5:24 pm, Lee <REM0VElbspamt...@cox.netwrote:
merlin_at@merlinsrealm_dot_net said:


Okay, I'm still really new to javascript, so I'm looking for a little help. I
want to grab a name with I'm trying to display a hyperlink in my body, but one
of
my functions overwrites it. The code is below. What am I doing wrong?
Thanks!
David
<html>
<head>
<title>
</title>
<script language="javascript" type="text/javascript">
var userName = "";
var randomMessage = "";
function loadPage()
{
userName = prompt("What is your name?", "Please enter your name");
if ((userName == "Please enter your name") || (userName == null))
{
userName = "Anonymous User";
}
document.writeln('Welcome to my web page, ' + userName);
}

1. You didn't explain clearly what it is that you're trying to do.
2. You included far too much whitespace in the code you posted.
As you can see, this makes it wrap and become difficult to read.
3. You used document.writeln() in a page that has already been
rendered. This clears the page and writes new content.

--
As Lee explained it in his 3. sentence, you rewrite your document so
none of the previous contents is left. Here's the thing - you can use
document.write(ln) in two situations:
1. <script>
function loading()
{
document.write( "bla" );
}
</script>
<body onload="loading()">
.....
</body>
In this situation, after the whole document is loaded, it executes the
given function. BUT, between these two steps the document does an
implicit .close() method of itself, so you can't write on it anymore.
If you still try, however, like we did in the loading() function, then
the document is reopened by an implicit .open() method, which rewrites
all of its contents.
2. <body>
..........
<script>
document.write( "bla" );
</script>
..........
</body>
In this situation, however, the document is parsed line-by-line, as
usual, and when it comes upon the <scriptblock, the document hasn't
yet finished with loading, thus it hasn't yet called the
implicit .close() method, and document.write( "bla" ) acts as we
wanted - it just inserts the "bla" string in the place where the
<scriptblock is. After that the rest of the document is loaded, and
we can see all the contents as we wanted it.

What you're trying to do shouldn't use document.write at all. You
should make a <div id="bla_place"></divelement in the document,
which is (as you can see) empty of contents but has an identifier
"bla_place". The loading (<body onload="loading()">) function should
look something like this:
function loading()
{
// ... you get the name by either prompt() or whatever
var bla_place = document.getElementById( "bla_place" );
bla_place.innerHTML = "Hello, " + name + "!";
}

You could of course use the DOM, instead of innerHTML, but for trivial
tasks such as this, it's really not necessary.

May 24 '07 #3
Sorry about the whitespace, I copied & pasted from my editor. What I'm trying to
do is simply display 2 things. The first is just a basic "Welcome to my web
site, " with the name the user enters onload. Then I just want to display a
hyperlink that includes onMouseOver(), onMouseOut(), and onClick(). I found out
that write() and writeln() clear the page, so what other options are there?

Thanks again!
~ David
--
--------------------------------- --- -- -
Posted with NewsLeecher v3.9 Beta 1
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

May 24 '07 #4
On May 25, 12:24 am, merlin (merlin_at@merlinsrealm_dot_net) wrote:
Sorry about the whitespace, I copied & pasted from my editor. What I'm trying to
do is simply display 2 things. The first is just a basic "Welcome to my web
site, " with the name the user enters onload. Then I just want to display a
hyperlink that includes onMouseOver(), onMouseOut(), and onClick(). I found out
that write() and writeln() clear the page, so what other options are there?
Maybe better if you quote previous messages.
<html>
<html>
<head>
<title>
</title>
<script language="javascript" type="text/javascript">
var userName = "";
var randomMessage = "";
function loadPage()
{
userName = prompt("What is your name?", "Please enter your name");
if ((userName == "Please enter your name") || (userName == null))
{
userName = "Anonymous User";
}
document.writeln('Welcome to my web page, ' + userName);
// As Darko suggested, use div element to display the message.
document.getElementById('msgArea').innerHTML = 'Welcome to my web
page, ' + userName;
}

function mouseIsOver(site)
{
mouseIsOverMessage = "This link will take you to www." + site;
// Use div to display messages too..
document.getElementById('msgArea').innerHTML = mouseIsOverMessage;
}

function mouseIsOut(site)
{
mouseIsOutMessage = "Are you sure you don't want to give www." +
site + " a shot?";
window.status = mouseIsOutMessage;
// this one too..
document.getElementById('msgArea').innerHTML = mouseIsOutMessage;
}

function mouseHasClicked(site)
{
randomNumber = Math.floor(Math.random()*5);
switch (randomNumber)
{
case "0":
// type of randomNumber is number, try test it with alert(typeof
randomNumber);
// so remove double quotes surrounding the number
case 0:
randomMessage = "Hang onto your butt, we're heading
to www." + site + "!";
break;
case "1":
// this one too..
case 1:
randomMessage = "Destination: www." + site;
break;
case "2":
case 2:
randomMessage = "www." + site + "? Energize!";
break;
case "3":
case 3:
randomMessage = "Are you ready to visit www." + site
+ "? Here we go!";
break;
case "4":
case 4:
randomMessage = "So, you think www." + site + " is
better?! Take a look";
break;
}
alert(randomMessage);
}
</script>
</title>
</head>
<body onLoad="loadPage();">
<h1 align="center">
Here are a few of my favorite links
</h1>
<hr />
<a href="http://www.slashdot.org" onMouseOver="mouseIsOver
('slashdot.org');" onMouseOut="mouseIsOut('slashdot.org');"
onClick="mouseHasClicked('slashdot.org');">
Slashdot
</a>
<!-- div to display the message -->
<div id="msgArea"></div>
</body>
</html>
Hope this help.

May 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marc | last post by:
Hi all, I am trying to write an application where I need the ability to open an Excel spreadsheet and do basic read/write, insert rows, and hide/unhide rows. Using win32com I have been able to...
4
by: Derek | last post by:
Hi, I've built a rather large CGI that dumps a lot of data and a fairly complex javascript app out to the client's browser. Granted this may be poor style according to someone web design...
5
by: Mitch | last post by:
Built a basic word game for the kids, using if, else , string input, etc, its kinda cool I would like to put in some bmp or jpg to color it up a bit. The books i have do not show how to display a...
2
by: Carole MacDonald | last post by:
There have been lots of posts on this topic, but I haven't been able to apply any of the suggestions to my situation. I have an .aspx page with a form on it that has several submit buttons on...
5
by: Robert | last post by:
Hello Accessors I have some reports created in Access that are very good for what they do. However, it seems to me that when you are displaying information you don't need to print out that a...
14
by: alwayshouston | last post by:
Hi All! I am working on this very small database and I am confused in the designing a simple form. I only have three tables in the database. First Table: tblExpense Columns: ExpenseID ;...
15
by: shaqattack1992-newsgroups | last post by:
Hello Everyone, Let me explain my problem. I have included 2 dashes between each pair of records to make it easier to see what goes together. In reality, it is just a long list of results...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
19
by: Ganesh J. Acharya | last post by:
Hi there, I want to redesign my website and make that look professional. I made this about 6 years ago with very little knowledge of internet. Today I am getting about 4000 visitors a day for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.