Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function issue JQuery 1

Status
Not open for further replies.

craigward

Programmer
Nov 13, 2007
230
0
0
GB
Hi there,

I am very new to JQuery and found a great function that adds days to a date. Here is the link to the working Jfiddle example


I took this code so that I could test it locally but it will not function. The error I am getting is

SCRIPT5009: '$' is undefined test.html (19,1)
SCRIPT7002: XMLHttpRequest: Network Error 0x3, The system cannot find the path specified.

Here is the source that I took from the Jfiddle example

Any advice on this would be really helpful as I have been chasing my tail for a while now.

Thanks for looking.

Code:
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="robots" content="noindex, nofollow">
  <meta name="googlebot" content="noindex, nofollow">
  <script type="text/javascript" src="//code.jquery.com/jquery-2.1.0.js"></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <title></title><script type='text/javascript'>//<![CDATA[
$(window).load(function(){
;(function($, window, document, undefined){
    $("#days").on("change", function(){
       var date = new Date($("#start_date").val()),
           days = parseInt($("#days").val(), 10);
        
        if(!isNaN(date.getTime())){
            date.setDate(date.getDate() + days);
            
            $("#end_date").val(date.toInputFormat());
        } else {
            alert("Invalid Date");  
        }
    });
    
    
    //From: [URL unfurl="true"]http://stackoverflow.com/questions/3066586/get-string-in-yyyymmdd-format-from-js-date-object[/URL]
    Date.prototype.toInputFormat = function() {
       var yyyy = this.getFullYear().toString();
       var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
       var dd  = this.getDate().toString();
       return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]); // padding
    };
})(jQuery, this, document);
});//]]> 

</script>

  
</head>

<body>
  <input type="date" id="start_date" placeholder="Start Date"/>
<input type="number" id="days" placeholder="Days"/>
<input type="date" id="end_date" placeholder="End Date" readonly/>
  
  <script>
  // tell the embed parent frame the height of the content
  if (window.parent && window.parent.parent){
    window.parent.parent.postMessage(["resultsFrame", {
      height: document.body.getBoundingClientRect().height,
      slug: "MCzJ6"
    }], "*")
  }
</script>

</body>

</html>
 
Hi

The code you posted works for me.

The code you posted may be irrelevant as the 2[sup]nd[/sup] error talks about [tt]XMLHttpRequest[/tt] which not seems to be related.


Feherke.
feherke.github.io
 
Hi, thank you for the reply.

Could you tell me what browser you are using please?
 
Hi

[ul]
[li]Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0[/li]
[li]Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/61.0.3163.100 Chrome/61.0.3163.100 Safari/537.36[/li]
[/ul]
Also parsed with my good old pair of eyes and they also confirmed there can be no such error like those you quoted.


Feherke.
feherke.github.io
 
Thanks for the reply. Very strange, in IE, Edge and firefox the example here works fine but with the sample code I pasted and load locally, the end_date does not get set when I add some days.

I will keep trying to troubleshoot. Thanks again for looking.
 
Hi

Ah ! I think I got what you did. You open it directly from the file system with [tt]file:[/tt] protocol. For me it worked because I requesting it from my local HTTP server as [ignore][/ignore].

Then your problem is with the protocol-less URL :
Code:
[gray]<!--[/gray]
[gray]no protocol specified to inherit from the page :[/gray]
[gray]  * when used in page [URL unfurl="true"]http://jsfiddle.net/MCzJ6/1/[/URL] jQuery will be loaded from [URL unfurl="true"]http://code.jquery.com/jquery-2.1.0.js[/URL][/gray]
[gray]  * when used in page http[b]s[/b]://jsfiddle.net/MCzJ6/1/ jQuery will be loaded from http[b]s[/b]://code.jquery.com/jquery-2.1.0.js[/gray]
[gray]  * when used in page file://yourhome/test.html will fail[/gray]
[gray]-->[/gray]
[b]<script[/b] [maroon]type[/maroon][teal]=[/teal][i][green]"text/javascript"[/green][/i] [maroon]src[/maroon][teal]=[/teal][i][green]"//code.jquery.com/jquery-2.1.0.js"[/green][/i][b]></script>[/b]

[gray]<!--[/gray]
[gray]protocol specified :[/gray]
[gray]  * when used in page [URL unfurl="true"]http://jsfiddle.net/MCzJ6/1/[/URL] jQuery will be loaded from [URL unfurl="true"]http://code.jquery.com/jquery-2.1.0.js[/URL][/gray]
[gray]  * when used in page http[b]s[/b]://jsfiddle.net/MCzJ6/1/ some browsers will refuse to load mixed in content from insecure source[/gray]
[gray]  * when used in page file://yourhome/test.html jQuery will be loaded from [URL unfurl="true"]http://code.jquery.com/jquery-2.1.0.js[/URL][/gray]
[gray]-->[/gray]
[b]<script[/b] [maroon]type[/maroon][teal]=[/teal][i][green]"text/javascript"[/green][/i] [maroon]src[/maroon][teal]=[/teal][i][green]"[highlight]http:[/highlight]//code.jquery.com/jquery-2.1.0.js"[/green][/i][b]></script>[/b]

Feherke.
feherke.github.io
 
Thank you feherke that was exactly the issue! Really appreciate the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top