SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    placing javascript function in a seperate file

    Hello,

    I have the following functions that will allow me to add and subtract values from a label. I can get it to work fine when the javascript is inserted in to the head of the page

    Code:
    <head runat="server">
        <script>
        function AddQuantity() {
            var Quantity = document.getElementById('Quantity');
            var currval = parseFloat(Quantity.innerHTML);
            Quantity.innerHTML = currval + 1;
        }
        function SubtractQuantity() {
            var Quantity = document.getElementById('Quantity');
            var currval = parseFloat(Quantity.innerHTML);
            if (currval != 0)
            {
                Quantity.innerHTML = currval - 1;
            }
        }
        </script>
    </head>
    however, when I try and alert this so that the js function are in a seperate file, othing will work.

    head of page:
    Code:
    <head runat="server">
        <SCRIPT LANGUAGE="JavaScript" SRC="Javascript.js"></SCRIPT>
    </head>
    javascript.js
    Code:
        function AddQuantity() {
            var Quantity = document.getElementById('Quantity');
            var currval = parseFloat(Quantity.innerHTML);
            Quantity.innerHTML = currval + 1;
        }
        function SubtractQuantity() {
            var Quantity = document.getElementById('Quantity');
            var currval = parseFloat(Quantity.innerHTML);
            if (currval != 0)
            {
                Quantity.innerHTML = currval - 1;
            }
        }
    WHen running in firefox, the javascript console is telling me that:
    AddQuantity is not defined
    SubtractQuantity is not defined

    Where am i going so wrong?

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You got any url where this can be seen? Do view-source in Firefox, what do you see?

  3. #3
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im affraid there is not url where is can been seen at the moment.

    Viewing the source in firefox:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head><title>
    	Untitled Page
    </title>
        <SCRIPT LANGUAGE="JavaScript" SRC="Javascript.js"></SCRIPT>
        
        <script>
    
    
        </script>
    </head>
    <body>
        <form name="form1" method="post" action="Default2.aspx" id="form1">
    
    <div>
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTA0MzUwODU0Ng9kFgICAw9kFgQCAw8PZBYCHgdvbmNsaWNrBQ1BZGRRdWFudGl0eSgpZAIFDw9kFgIfAAUSU3VidHJhY3RRdWFudGl0eSgpZGREycCRvz8Zuoc2RjFIHBfxvI991w==" />
    </div>
    
    
        <div>
            <span id="Quantity">0</span>
            <img id="addtoquantity_image" onclick="AddQuantity()" src="images/arrow/arrowup.GIF" style="border-width:0px;" />
            <img id="subtracttoquantity_image" onclick="SubtractQuantity()" src="images/arrow/arrowdown.GIF" style="border-width:0px;" />
            </div>
        </form>
    
    </body>
    </html>

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks alright, what does the JavaScript console in Firefox say when you load that page?

  5. #5
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when the page load, the js console does not say anythin.
    Its when one of the images are clicked, the following is inserted into the js console:

    AddQuantity is not defined
    SubtractQuantity is not defined

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Somehow it is not loading the JavaScript file properly. Why I don't know. One theory was that js files were loaded using the incorrect mimetype, but the console should have said anything about that.


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
  •