Javascript JS file

Hello expert,

Need to use an external JS file to write to a file.

Made an HTML file and JS file to discover how to connect.

FIles below.

Used the document. write method before but have been told that
the document.write is old fashioned and that document.getElementById with innerHTML
is the way to go. So in the HTML file I did ocument.getElementById with innerHTML
method using cmsDateTime to write to the page.

Then the same method was used to write from the JS file to the HTML file
using cmsDateTime2 to write to the page but no luck.

Thought that perhaps the function writeDate in the JS file needed to be called from within
the HTML page to get the definition of cmsDateTime2 into memory. Tried calling
writerDate two ways but no luck.

Why will the div id in the HTML page not see the definition in the JS file?

Thanks.

Allen in Dallas





+++++++++++HTML file code begin +++++++++++++++++
<html>
<head>
<script src="js_file_external40728.js">
</script>

<style>
body {font-family:"Arial";
}
</style>

</head>
<body onload = writeDate()>
</head>
This file uses and external JS file.

<body>

<div>
<script type="text/javascript">
window.onload=function(){
document.getElementById('cmsDateTime').innerHTML = "LHP Hospital Group";
}
</script>
<div id = "cmsDateTime")
<script type="text/javascript">
writeDate()
</script>
<div id = "cmsDateTime2")
</div>
</body>
</html>

+++++++++++HTML file code end +++++++++++++++++

+++++++++++JS file code begin +++++++++++++++++
function writeDate() {
document.getElementById('cmsDateTime2').innerHTML = "LHP Hospital Group Collaboration";
}
+++++++++++JS file code end +++++++++++++++++
Allen PittsBusiness analystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Question is a bit confusing with your code.
So the writeDate() function is in your js file?

This is your code with the syntax errors corrected

<html>
 <head>
 <script src="js_file_external40728.js">
 </script>

 <style>
 body {font-family:"Arial";
 }
 </style>

 </head>
<body onload = writeDate()>

 This file uses and external JS file.

 <body>

 <div>
 <div id = "cmsDateTime"> </div>
     <script type="text/javascript">
 window.onload=function(){
 document.getElementById('cmsDateTime').innerHTML = "LHP Hospital Group";
 }
 </script>
 </body>

Open in new window

0
Allen PittsBusiness analystAuthor Commented:
Gary,

The file should write
'LHP Hospital Group' from the element id
'cmsDateTime within the HTML.

There is also a second div
with id 'cmsDateTime2' which is defined
in the JS file 'LHP Hospital Group Collaboration'  

The first string, 'LHP Hospital Group',  writes
the second 'LHP Hospital Group Collaboration'  
does not.

Thanks.

Allen Pitts
LHP Hospital Group
0
GaryCommented:
<html>
 <head>
 <script src="js_file_external40728.js">
 </script>

 <style>
 body {font-family:"Arial";
 }
 </style>

 </head>
 <body onload = writeDate()>

 <body>

 <div id = "cmsDateTime"></div>

 <div id = "cmsDateTime2"></div>

<script>
 window.onload=function(){
 document.getElementById('cmsDateTime').innerHTML = "LHP Hospital Group";
 }
 </script>

 </body>
</html>

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Allen PittsBusiness analystAuthor Commented:
Hello Gary,

Thanks for your help.

I see it writing to
<div id = "cmsDateTime"></div>
but the text for
<div id = "cmsDateTime2"></div>
does not appear.

Sorry if the explanation was not clear.
The writing of 'LHP Hospital Group Collaboration'
is the issue. If the 'cmsDateTime' can be used
in the script to write to the div with id 'cmsDateTime'
why will the script in the external file not write
to the div with id 'cmsDateTime2'.

I think it may be because innerHTML is a DOM object
so using it in an external JS file does not make
the cmsDateTime2 attribute available?

Is that right. If so how to get cmsDateTime2  
into the page HTML?

Thanks

Allen
0
GaryCommented:
<html>
 <head>
 <script src="js_file_external40728.js"></script>
 <style>
 body {font-family:"Arial";
 }
 </style>

 </head> 
 <body>

 <div id = "cmsDateTime"></div>

 <div id = "cmsDateTime2"></div>

<script>
 window.onload=function(){
 document.getElementById('cmsDateTime').innerHTML = "LHP Hospital Group";
 writeDate()

 }
</script>

</body>
</html>

Open in new window

0
Ray PaseurCommented:
Sometimes it's easier to "conquer JavaScript" with jQuery.  It's a framework that implements the good parts of JavaScript in a sensible and cross-browser-compatible manner.  This script waits for your click, but it would be just as easy to make the document.ready() load these divs immediately if that is what you want.

Please see: http://iconoun.com/demo/temp_9apit.php

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
    $("#signal").click(function(){
        $("#d1").text('LHP1');
        $("#d2").text('LHP2');
    });
});
</script>

<style type="text/css">
#signal {
    color:red;
    font-family:verdana;
    font-weight:bold;
}
</style>

<title>HTML5 Page in UTF-8 Encoding</title>
</head>
<body>

<p id="signal">Click here to load information into these DIVs:</p>
<div id="d1">This is DIV #1</div>
<div id="d2">This is DIV #2</div>

</body>
</html>

Open in new window

0
Ray PaseurCommented:
Thanks for the points.  If you're interested in loading the divs from an external script, jQuery + AJAX is the usual solution.  This article shows the "hello world" exercise.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html
0
GaryCommented:
Why accept a comment that did not answer your question?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Allen PittsBusiness analystAuthor Commented:
Hello Gary,

Sorry, clicked the wrong button.

Allen
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.