4

This is an basic example of my question.

I have two or more html pages, look like

page1.html

<script src='notify.js'></script>
<h2>Page 1</h2>
<button id='btn' onclick='notify();'>Click</button> 

page2.html

<script src='notify.js'></script>
<h2>Page 2</h2>

and so on..

my js file is notify.js

function notify(){
alert("button clicked");
}

In page1.html, there is a button. When I click the button the alert message should show in all pages. How can I achieve this?

In page1.html, the alert message is shown successfully. But I need to show the alert message in all pages, whenever the button (in page1.html) is clicked.

This is like a notification process. I think some one can help me..

Deepu Sasidharan
  • 5,193
  • 10
  • 40
  • 97

4 Answers4

1

well this task requires you to set up some messaging across your pages. you can use your DB to store these messages and have a read flag attached to them.

so when a button in page1 is clicked you fire an ajax request that writes a new message to a DB table like this..

$.ajax({url: "/addAlertMessageToDB.php", success: function(result){
       alert("message written to DB");
    }});

then in every other page that you want the alert to be shown you should have a JS ajax request that using setInterval looks for any new messages

setInterval(function(){ 

    $.ajax({url: "/getLatestMessage.php", success: function(result){
            alert(result);
        }});


}, 3000);

in the example above you see an ajax request fired every 3 seconds looking for new messages in your DB.

and your server side code should query something like this ..

select id,message,readFlag from messagesTable where readFlag = false

then you should return a result that you need to alert according to the messages in the DB..

Mortalus
  • 10,574
  • 11
  • 67
  • 117
1

If you have multiple tabs of your application open in a browser you cannot communicate with them that easy. It wasn't designed with this purpose in mind because tabs in browsers came after JavaScript was invented.

Possible solutions would be:

  1. Send a message to the server via Ajax and in the other instances poll the server every x seconds to see if a message has been sent. The downside is unnecessary network usage and performance for your backend but easy to implement.
  2. Use websockets. It was created for this purpose but it take time to understand the principles and to set it up. But this is currently the best solution for "push" for web applications.
ST2OD
  • 705
  • 1
  • 5
  • 15
0

There are different approaches:

  1. Store the message in DB and use Long Polling or Forever Frame.
  2. Ajax call periodically and get it from DB.
  3. Use HTML5 WebSocket.
  4. SignalR (In case of .Net ), it uses the WebSocket transport where available and falls back to older transports where necessary.

Choose the option whatever most applicable to you. Difference between web sockets, long polling, server-sent events and forever frame - click here for details.

Community
  • 1
  • 1
Sudipta Kumar Maiti
  • 1,669
  • 1
  • 12
  • 18
-1

as you said "notification", This is really process like messaging between pages either on same domain or cross,

If So :- Then you have to think about realtime messaging such as XMPP,websockets,sockets etc the process will be

1 listner as well as 1 publisher on each page

listner will listen to notification and react publisher will broadcast notifications(your button)

anshuVersatile
  • 2,030
  • 1
  • 11
  • 18