Day: February 12, 2018

Get information from the web server automatically

Server-sent events is a technology for where a browser gets automatic updates from a server via HTTP connection. The Server-Sent Events EventSource API is standardized as part of HTML5[1] by the W3C.

Server-Sent Events (SSE) is a standard describing how servers can initiate data transmission towards clients once an initial client connection has been established. They are commonly used to send message updates or continuous data streams to a browser client and designed to enhance native, cross-browser streaming through a JavaScript API called EventSource, through which a client requests a particular URL in order to receive an event stream. First create a simple html page which call php file and get result from this file

<!DOCTYPE html>
<h1>Getting server updates</h1>
<div id="result"></div>

        var source=new EventSource("http://localhost/result.php");
            document.getElementById("result") + "<br>";
        document.getElementById("result").innerHTML="Sorry, your browser does not support server-sent events...";


then create result.php

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

$time = date('r');
echo "data: The server time is: {$time}\n\n";

Then you can see the result, without page refresh time will update

This is simple demo result

If you face any problem then please inform me. I will try to help you.


how to remove http:// or https:// from url using JavaScript?

In some case we need to remove https or http from url. We can do it using javascript. Here is simple code

   var url = "";
   url = url.replace(/^https?:\/\//,'');

This output :

If you face any problem then please inform me, I will try to help you.