SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery and the browser

    can anyone explain to me why I would be unable to test pages that include jQuery code locally in my browser? (Safari 5.0.6 and Firefox 3.6.28) It seems I need to upload the page to a web server for testing results. The pages include a link to a jQuery external library and work for the most part. Some features don't seem to work. Do browser compatibility issues exist?

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,354
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Hi there,

    How are you including jQuery?

    Often it is done like this:

    HTML Code:
    <script src="//code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    and this won't work locally.

    The local variation would be:

    HTML Code:
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    Otherwise, if you could post a bit of code that works on your server, but not locally, that would help diagnose things further.

  3. #3
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm going through some exercises in a book. The following is an example of a script I'm trying to run. Again, the functionality appears when run from a server but not when run locally. Also, I can't seem to get this simple exercise to run correctly. The script never gets beyond the first IF conditional because it doesn't see any numerical values entered. I noticed, that the 'Miles Per Gallon' form element should reflect the calculated value when the script runs correctly but the values don't persist on the form, the elements immediately reset themselves. How do I get the values to remain on the form when the script completes?

    HTML Code:
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <style type = 'text/css'>
    </style>
    
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    [COLOR="#808080"]<!--<script type="text/javascript" src="jquery-1.7.2.min.js"></script>-->[/COLOR]
    <script type='text/javascript'>
    
     $(document).ready(function()
        { $('input').change(function()
            { var startMiles = parseInt($('#startingMiles').attr('value'));
              var endMiles = parseInt($('#endingMiles').attr('value'));
              var gallons = parseInt($('#gallonsUsed').attr('value'));
              if (isNaN(startMiles) || isNaN(endMiles) || isNaN(gallons))
                window.alert('you must enter numeric values!');
              else
                if (gallons > 0)
                    { var mpg = (endMiles - startMiles) / gallons;
                      $('#mpg').attr('value', mpg); } }); });
          
    </script>                    
    </head>
    
    <body>
    <h1>Gas Mileage</h1>
    <form action = ''>
        <p>Starting Mileage<input type = 'text' id = 'startingMiles' value = '0' /></p>
         <p>Ending Mileage<input type = 'text' id = 'endingMileage' value = '0'/></p>
         <p>Gallons Used<input type = 'text' id = 'gallonsUsed' value = '0'/></p>
         <p>Miles Per Gallon<input type = 'text' id = 'mpg' value = '0'/></p>
    </form>    
    </body>
    </html>
    Last edited by cpradio; May 3, 2013 at 04:47.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,608
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Simply change all the src="// references to src="http// as Pullo already advised
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, that small revision seemed to clear up my other problems as well. All set for now.


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
  •