Get time from time.nist.gov NTP server using JavaScript? - javascript

How to do? I am a beginner coder - full references and code would help. I literally spent like 5 hours trying to find a solution to this - there are some references online but nothing works! And I don't have access to the NTP server and yes I have to use a public server - such as time.nist.gov.
Help!!!

Short answer:
Not doable.
Long answer:
Even if cross-origin policy would allow it, there's no way to get NTP directly via Ajax without PHP (or something) relaying your request. First reason is that time servers normally stay on UDP port 123; there's no way for Ajax to do UDP; if that's not enough, when Ajax sends a request to a server, it expects to see in response some HTTP headers, some status codes, a response body, etc. NTP doesn't keep that structure, it only sends a string. And there is no raw socket connection support in HTML5 either.
But what you can do with Ajax is look at the request headers because most responses come back with a header that looks like this:
Date:Mon, 21 May 2012 15:30:58 GMT
And there's your time.

Related

node.js response.writeHead on http module

I'm implementing my own http module.
As I'm reading the official node.js http module api, I couldn't understand a few things:
If the user is using the response.writeHead(statusCode, [reasonPhrase], [headers]) function, are the headers should be written immidiatly to the socket or are they first supposed to be saved as a member to the object? and then written only after .end() function?
What is the meaning of implicit headers that should be used whenever the user didn't use writeHead()? are they supposed to be set ahead? and if the user didn't set them? what should be the behavior? thanks
Answers:
Anything that you write into response either headers with writeHead or body with write is buffered and sent. You see they use socket buffers. They can only hold fixed amount of data, before being sent. The important fact to remember is that you can only set headers before you start writing the body. If you do, some headers will set for you by the http server itself.
Implicit headers are ones which you don't write specifically but are still sent. Setup a simple http server, by responding a request without setting any header. Then view the request headers opening the site in browser. There will be headers like Date, Server, Host etc which are added to every request automatically without user's volition.
I found answer for the first question, but still don't understand the second one.
The first time response.write() is called, it will send the buffered header information and the first body to the client. The second time response.write() is called, Node assumes you're going to be streaming data, and sends that separately. That is, the response is buffered up to the first chunk of body.

How can I make page reload everytime I open it

I'm making kind of social website. And i make a post edit page. The problem is when i finish edit post and click 'SAVE EDIT'. i use
window.location='post_info.php?post_id='+postid; on AJAX to go back to post info page.
But it appear to be old content from cache.
question is what should i do to make my new post info appear immediatly when open it.
You just need to set the cache control HTTP headers for your HTML document so that the document is always loaded from the server and not the browser (or proxy) cache.
You can't do this with JavaScript or HTML (meta http-equiv is a joke), you need to set real HTTP headers.
Either
Cache-Control: max-age=0
or
Cache-Control: no-cache
should get you what you want. See this answer for an explanation of the differences between them and Mnot's caching tutorial for a more in depth look at caching and HTTP.
You can set these with a server side programming language (e.g. Perl, Python or PHP) or by configuring the web server directly. The specifics depend on which of those you choose.
you should try this:
Use Category: Shorthand Methods: AJAX ajax
Change your code to
window.location='post_info.php?post_id='+postid+"&"+ new Date().getTime();

How to cache an HTTP response indefinitely

How do I send an HTTP response that any client would cache forever (or until its cache is cleared), such that when the browser needs that resource, it makes no HTTP request and instead retrieves the HTTP response from the local file system?
Usage note: this is for versioned client code in an Ajax application. Everything is accessed through the uncacheable example.com/front.htm, which merely contains a script tag linking to example.com/currentversion/bootstrap.js which is cached indefinitely. Because the uncacheable HTML file determines the current version, there is no need for the client to update scripts.
According to the RFC, "to mark a response as 'never expires,' an origin server sends an Expires date approximately one year from the time the response is sent. HTTP/1.1 servers SHOULD NOT send Expires dates more than one year in the future."
See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21.
I don't know what the reasoning behind the one-year limitation is, so take it for what it is.
Cache until 2038. You can't go any farther than that (reliably) because of the 32-bit Unix Epoch bug. Use this header:
Expires: Sun, 17 Jan 2038 19:14:07 GMT

How can I get time from another server using AJAX?

I need to get time from some online source - NTP server, JSON time server, or maybe event just an HTTP Header. I don't care about precision, but I do need to get time from an online source. I don't have access to any servers so I can't write any server side code.
I can't do this using HTTP Request to get the header since I guess it would be violating the same origin policy.
And I can't seem to find a way of doing it that does not involve some level of PHP or other languages - I can only use HTML/CSS/JS...nothing else!
Any ideas?
$.ajax({
url: 'http://timeapi.org/utc/now.json',
dataType: 'jsonp'
})
.done(function(response) {
// response = {"dateString":"2012-03-06T02:18:25+00:00"}
console.log(response.dateString);// "2012-03-06T02:18:25+00:00"
});
JavaScript has a same origin policy. You can not make calls to another domain. If you could do it, I could access your email, bank accounts, etc.
If the browser supports CORS and the 3rd party has it enabled, you are in luck. Other than that, you should look at a JSONP service that can give you the info.
Javascript has built-in functionality for date and time. You can get the current time like so:
var now = new Date();
document.write(now);
You can read more about using Javascript's Date object here http://www.w3schools.com/js/js_obj_date.asp

Cross-domain website promotion

I'd like to offer a way to my users to promote my website, blog etc. on their website.
I can make a banner, logo whatever that they can embed to their site, but I'd like to offer dynamic content, like "the 5 newest entry's title from my blog".
The problem is the same origin policy. I know there is a solution (and I use it): they embed a simple div and a JavaScript file. The JS makes an XmlHttpRequest to my server and gets the data as JSONP, parses the data and inserts into the div.
But is it the only way? Isn't there a better way I could do this?
On the Internet there are tons of widget (or whatever, I don't know how they call...) that gain the data from another domain. How they do that?
A common theme of many of the solutions, instead, is getting JavaScript to call a proxy program (either on the client or the server) which, in turn, calls the web service for you.
The output can be written to the response stream and then is available, via the normal channels, such as the responseText and responseXML properties of XMLHttpRequest.
you can find more solution here :
http://developer.yahoo.com/javascript/howto-proxy.html
or here :
http://www.simple-talk.com/dotnet/asp.net/calling-cross-domain-web-services-in-ajax/
CORS is a different way than JSONP.
Plain AJAX. All your server has to do is to set a specific header: Access-Control-Allow-Origin
More here: http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/
If you go the JSONP route, you will implicitly ask your users to trust you, as they will give you full access to the resources of their page (content, cookies,...). If they know that they main complain.
While if you go the iframe route there is no problems.One famous example today of embeddable content by iframe is the Like button of facebook.
And making that server side with a proxy or other methods would be much more complex, as there are plenty of environments out there. I don't know other ways.
You can also set the HTTP Access-Control headers in the server side. This way you're basically controlling from the server side on whether the client who has fired the XMLHttpRequest is allowed to process the response. Any recent (and decent) webbrowser will take action accordingly.
Here's a PHP-targeted example how to set the headers accordingly.
header('Access-Control-Allow-Origin: *'); // Everone may process the response.
header('Access-Control-Max-Age: 604800'); // Client may cache this for one week.
header('Access-Control-Allow-Methods: GET, POST'); // Allowed request methods.
The key is Access-Control-Allow-Origin: *. This informs the client that requests originating from * (in fact, everywhere) is allowed to process the response. If you set it to for example Access-Control-Allow-Origin: http://example.com, then the webbrowser may only process the response when the initial page is been served from the mentioned domain.
See also:
MDC - HTTP Access Control

Categories

Resources