Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    114
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Pulling data from a .js file into a .html [RESOLVED]

    Hey there, a basic question for you experts out there.

    I have a site which organizes information into 'articles', or pages with information within them. On my homepage I am trying to do something very simple - write 'We currently have x articles' where x is the amount of articles.

    I currently have a .js file which has:
    Code:
    myArticle = new Array("Article1", "Article2", "Article3")
    artCount =  myArticle.length
    In my main page I have this for my basic code:
    Code:
    <html>
    <head>
    <title>My site</title>
    
    ...
    
    <script src="/member/javascript/articledirectory.js" language="Javascript" type="text/javascript">
    </script>
    
    </head>
    
    <body>
    
    ...
    
    We currently have 
      <script>
          <!--
          document.write(artCount)
          //  -->
       </script>
    articles.
    
    ...
    
    </body>
    </html>
    Any clues as to what I am doing wrong?
    Last edited by JoWiGo; 07-05-2005 at 10:02 PM.

  • #2
    New Coder
    Join Date
    Jun 2005
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code for your external Javascript file:
    Code:
    function articleCount() {
    myArticle = new Array("Article1", "Article2", "Article3")
    artCount =  myArticle.length;
    return artCount;
    }
    HTML Code:
    Code:
    <html>
    <head>
    <title>My site</title>
    ...
    <script language="Javascript" src="/member/javascript/articledirectory.js"></script>
    </head>
    <body>
    ...
    
    We currently have 
    <script>	  
         var artcountresult=articleCount();       
         document.write(artcountresult);     
    </script>
    articles.
    ...
    
    </body>
    </html>
    Hope this helps you out

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    114
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Err, unfortunatley it didn't. Running the page with that code returns an error. In front of
    Code:
         var artcountresult=articleCount();
    I recieve an error saying that the browser expects an object before var. Any clues?

  • #4
    New Coder
    Join Date
    Jun 2005
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The error is very likely a product of your relative reference to your external javascript file. When I tested the code, I had my "articledirectory.js" file in the SAME folder as my HTML document. I just put your URL for the External Javascript file back in. I can think of a few things that would cause this error.

    1) You did not copy the code correctly. Make sure you didnt leave something out, or accidently change something somewhere.

    2) You did not upload or update the External Javascript file with the new code. If it's already "online", re-upload the new javascript file and make sure that you reference it's location properly. The easiest way to make sure is to just have the External Javascript file in the same folder as your HTML document calling the Javascript. Change the src="/member/javascript/articledirectory.js" to src="articledirectory.js".

    The code works fine for me with the "articledirectory.js" file in the same folder as the HTML document calling the file, so I'm guessing the problem is on your end
    Last edited by Burgerking63; 07-05-2005 at 09:04 PM.

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    114
    Thanks
    4
    Thanked 3 Times in 3 Posts
    I have found out the problem, and it's fixed . The error was my HTML editor could not process teh script for some reason. However, I uploaded the files and everything worked out fine. Thanks for your patience!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •