SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2008
    Location
    Hot Springs, Montana
    Posts
    195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    n00b question on incorporating script

    Greetings, again. Please excuse the n00bness of this inquiry... I'm merely trying to insert two very basic scripts, but I'm confused on how to do it.

    Embedding the script directly into the doc is easy enough for my old brain. For example, I've embedded the following into my footer:
    Code:
    This document was last modified on:
    			<script type="text/javascript">
    			<!--
    				document.write(document.lastModified)
    			//-->	
    			</script>
    But, I'd like to put the script in a separate file in a folder in my root directory and call it from within my doc.

    I've created the script tag in the head [edit: from heading to head]
    Code:
    <script type="text/javascript" src="javascripts/cf_core.js"></script>
    And, I've created a javascript file called "cf_core."
    Code:
    /*     cf_core.js    */
    
    /*   LastUpdate function -- works in footer   */
    var LastUpdate =
    	{
    		init: function()
    		{
    			document.write(document.lastModified)
    		}
    	};
    Core.start(LastUpdate);
    /*   End of LastUpdate function  */
    /*   EmailCloak function -- works in footer   */
    var EmailCloak =
    	{
    		init: function()
    		{
    			/*  This function will cloak the e-mail adderss I use in the mailto: protocol  */
    		}
    	};
    Core.start(EmailCloak);
    /*   End of EmailCloak function  */
    /*   Core.start function delays javascript loading until html has loaded */
    Core.start = function (runnable)
    	{
    		Core.AddEventListener (window, "load", runnable.init);
    	};
    So, my question is: How do I make the connection between the code in the script file (cf_core.js) to the particular elements I want to affect? For instance, I'll have an element in my footer that tells the reader when I last revised the document.

    Or, for instance, I'll have a "contact me now" (or something similar) link on my left sidebar. (It will be within an li element within a ul, within the "left_sidebar" div.) How do I get that link to reflect/embody (lacking a term.... "call"?) the function? In otherwords, how do I connect the li item (containing the "contact me" link) to the function/script that is named in the head?

    BTW, for background: I'm trying to finish writing a very simple web site (xhtml and css) with a minimum of javascript. My plan is to get the site up this week, then immerse myself (back) into javascript (with the intention of gaining a basic understanding... enough to avoid merely "drinking the kool-aid").

    (OFF TOPIC: BTW, surely the phrase, "drinking the kool-aid" refers to certain institutions created by the merry pranksters?)

    Thanks again for answering what must seem to be a pretty silly question.
    Last edited by rbfree; Feb 6, 2008 at 17:08. Reason: Changed "heading" to "head"

  2. #2
    SitePoint Addict
    Join Date
    Apr 2006
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll need to create functions in your cf_core.js file to perform the actions you want - for example, perhaps a "function LastUpdated()" that when triggered uses document.write to print out the last modified date. Then, where you want it to show up in your page, use <script> tags to call that function.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2008
    Location
    Hot Springs, Montana
    Posts
    195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks for reply, may i ask for clarification

    EDIT: Resolved. I realized I needed to break it way back down into baby steps and the solution became clear. I still have some steps to get it all back into shape, but at least the path is clear. Thanks for all.

    I think I have it right, but could someone check my thinking?

    1. Keep the script tag in the head.
    2. Add script tags in the body, with the function coded in the separate cf_core.js file.
    3. The head script enables the browser to refer to the cf_core.js file when called by the script element in the body?

    It seems to me that this approach is unobtrusive. Is that a correct understanding?

    EDIT: Actually, I don't think I have it right at all. I can't seem to achieve the goal of calling this function and in all the zillion books I've acquired, I can't find a simple reference on how to do this or what syntax to use. I guessed at the below, but rather fruitlessly (with variations like "function Lastupdate" etc..)
    Code:
    <script type="text/javascript">
       write.LastUpdate
    </script>
    This all brings up a question for me. It seems like I would be just as well off to simply use a simple embedded javascript method, like:
    Code:
    <script type="text/javascript">
      <!--
    	document.write(document.lastModified)
      //-->	
    </script>
    Would this be problematic? It seems much more straightforward and direct. Thanks, again!
    Last edited by rbfree; Feb 7, 2008 at 11:27.

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like it should be:
    Code:
    <script type="text/javascript">
      <!--
    	LastUpdate.init()
      //-->	
    </script>

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2008
    Location
    Hot Springs, Montana
    Posts
    195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks

    Jim,

    Thanks for the tip. I figured it out by breaking it down into baby steps with minimal script. I should have posted a note instead of editing my message above.

    Bit by bit.


Bookmarks

Posting Permissions

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