Link to home
Start Free TrialLog in
Avatar of Allen Pitts
Allen PittsFlag for United States of America

asked on

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 +++++++++++++++++
Avatar of Gary
Gary
Flag of Ireland image

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

Avatar of Allen Pitts

ASKER

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
<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

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
<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

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

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.
https://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello Gary,

Sorry, clicked the wrong button.

Allen