PostMessage
API for communication.data-tab-opener="name"
attribute on the target link/button(which opens up a new tab), so that it remains to disable until Child
tab initiates a handshake and is received by the Parent
tab~4 KB
gzipped.$ npm install across-tabs
$ bower install across-tabs
<script>
tag<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/across-tabs/1.0.0/across-tabs.min.js"></script>
Note: Use the desired version of the library in place of 1.0.0
. Checkout cdnjs across-tabs.
P
) opens up a new Child tab(C
).C
initiates a handshake with the P
tab by sending a postMessage
.P
acknowledges the request and sends C
it's identity i.e. UUID
along with P
information.P
and C
can share custom messages with each other.C
gets closed/refreshed, P
is notified.P
is closed/refreshed, all children of P
tab gets notified.Explanation of diagram
P
) opens Child tab(C1
) at t=1
.c1a
- When C1
initiates a handshake with the Parent.P1
- When P
receives C1
message.P2
- P
acknowledges the request and sends the C1
its identity.c1b
- When C1
receives an acknowledgement message along with identity from P
.Total Tabs Associated: 1 | Opened Tabs: 1 | Closed Tabs: 0
P
) opens CHild tab(C2
) at t=10
.c2a
- When C2
initiates a handshake with the Parent.P3
- When P
receives C2
message.P4
- P
acknowledges the request and sends the C2
its identity.c2b
- When C2
receives acknowledgemnet message along with identity from P
.Total Tabs Associated: 2 | Opened Tabs: 2 | Closed Tabs: 0
c1c
- Tab C1
closes.P5
- P
is notified about the C1
. Parent updates the list.Total Tabs Associated: 2 | Opened Tabs: 1 | Closed Tabs: 1
P
) opens CHild tab(C3
) at t=25
.c3a
- When C3
initiates a handshake with the Parent.P6
- When P
receives C3
message.c2c
- Tab C2
sends a custom
message.P7
- When P
receives a message from tab C2
. It processes it.P8
- P
acknowledges the request and sends the C3
its identity.c3b
- When C3
receives acknowledgemnet message along with identity from P
.Total Tabs Associated: 3 | Opened Tabs: 2 | Closed Tabs: 1
P
is closed, all the opened tabs are notified about it.Opener(Parent) tab
var config = {
onHandshakeCallback: function () { ... },
onPollingCallback: function () { ... },
onChildCommunication: function () { ... }
}
var parent = new AcrossTabs.Parent(config);
Constructor
accepts a configurable Object
with the following keys.
heartBeatInterval
: The time interval for polling tabs statusesremoveClosedTabs
: Pass it as true
to have only opened tabs in the listshouldInitImmediately
: Pass it as false
to create an instance but initialize it lateronHandshakeCallback
: Callback to be called when a successful connection has been establishedonChildCommunication
: Callback to be called when child sends messageonPollingCallback
: Callback to be called every time a tab is polled for its statusorigin
: whitelist origin
for securing postMessage
communication. It will discard the malicious messages trying to trick the behavior. Eg. http://example.comparse
: parser used when parsing messages, defaults to JSON.parse
stringify
: stringifier used when converting data into messages, defaults to JSON.stringify
Config Keys | default | accepts |
---|---|---|
heartBeatInterval | 500 msec | A number representing milliseconds |
removeClosedTabs | false | Boolean |
shouldInitImmediately | true | Boolean |
onHandshakeCallback | Undefined | Function as callback |
onChildCommunication | Undefined | Function as callback |
onPollingCallback | Undefined | Function as callback |
origin | '*' | String(url) |
parse | JSON.parse | Function |
stringify | JSON.stringify | Function |
New(Child tab)
var config = {
onReady: onReady,
onInitialize: onInitialize,
isSiteInsideFrame: false, // dont set if not required
handshakeExpiryLimit: 4000, // msec
onParentDisconnect: onParentDisconnect,
onParentCommunication: onParentCommunication
};
var child = new AcrossTabs.Child(config);
Constructor
accepts a configurable Object
with the following keys.
handshakeExpiryLimit
: Time to wait for getting an acknowledgement from Parent window for child's requestonReady
: Callback to be invoked once child instance is readyonInitialize
: Callback when a child instance is actually initiatedonParentDisconnect
: Callback to be invoked when Parent gets disconnectedonParentCommunication
: Callback to be invoked whenever Parent communicates with the child tabisSiteInsideFrame
: If the library is loaded inside an iframe in the child tab, this needs to be set true
for maintaining proper window/frame(s) referencesorigin
: whitelist origin
for securing postMessage
communication. It will discard the malicious messages trying to trick the behavior. Eg. http://example.comparse
: parser used when parsing messages, defaults to JSON.parse
stringify
: stringifier used when converting data into messages, defaults to JSON.stringify
Config Keys | default | accepts |
---|---|---|
handshakeExpiryLimit | 5000 msec | A number representing milliseconds |
isSiteInsideFrame | null | If child tab has actual site in a fram |
onReady | Undefined | Function as callback |
onInitialize | Undefined | Function as callback |
onParentDisconnect | Undefined | Function as callback |
onParentCommunication | Undefined | Function as callback |
origin | '*' | String(url) |
parse | JSON.parse | Function |
stringify | JSON.stringify | Function |
Example is included in the example
folder. Vanilla JS
and Vue js
versions are there to test out.Note: Run npm install
if you wish to run vuejs
example since the example needs the vue-js
library to work.
Refer above section on how to create an instance before hitting API methods.
openNewTab
Saves data
in specific key
in sessionStorage. If the key is not provided, the library will warn.Following types of JavaScript objects are supported:
Parameter | Description |
---|---|
config | For opening a new tab i.e. URL and windowName |
parent.openNewTab({url: 'http://example.com', windowName: 'AcrossTab'});
enableElements
Links / buttons can be given a data attribute: data-tab-opener="name"
. On clicking, the library finds elements with that attribute and adds disabled="disabled"
attribute to that element. The disabled
attribute is removed once the Child tab communicates back to the opener i.e. Parent. Though, this method is called internally but can also be invoked(may be within a timer) to be on a safer side.
parent.enableElements();
getAllTabs
Returns the list of all the tabs.If removeClosedTabs: true
is provided while instantiating, it will return only the opened tabs as closed tabs would be removed.
parent.getAllTabs()
getOpenedTabs
Returns the list of all opened
tabs.
parent.getOpenedTabs();
getClosedTabs
Returns the list of all closed
tabs.If removeClosedTabs: true
is provided while instantiating, it will return empty list as closed tabs would be removed.
parent.getClosedTabs();
closeAllTabs
Closes all the opened tabs.
parent.closeAllTabs()
closeTab
Closes a particular tab whose id is provided.
Parameter | Description |
---|---|
id | id of the tab to be closed |
parent.closeTab('57cd47da-d98e-4a2d-814c-9b07cb51059c');
broadCastAll
Sends a same message
to all the opened tabs.
Parameter | Description |
---|---|
msg | msg to be ent |
parent.broadCastAll('Hello my dear Child! A greeting from Parent.');
broadCastTo
Sends a message
to a particular opened tab.
Parameter | Description |
---|---|
id | id of the tab to send an msg |
msg | msg to be sent |
parent.broadCastTo('57cd47da-d98e-4a2d-814c-9b07cb51059c', 'Hey! Can you run the script: worker.js? Thanks!');
getTabInfo
Return id, name, and parentName of the child tab.
child.getTabInfo();
sendMessageToParent
Sends a message
to the Parent
tab.
Parameter | Description |
---|---|
msg | msg to be sent |
child.sendMessageToParent('Hey Parent! I\'m done with my work.');
Tested in Chrome, Firefox and IE(versions >= 9).
src
compilation & bundling and dist
generation.ES6 source files
|
|
webpack
|
+--- babel, eslint
|
o--- tests and covergae
|
ready to use
library
in UMD format
npm run build
- produces production version(minified) of the library under the dist
foldernpm run dev
- produces development version(un minified) of the library and runs a watcher to detect file changes.npm run test
- well ... it runs the tests :)npm install
and npm run dev
.<fix-typo>
and do your work.npm run build
to build dist files and npm run test
to ensure all test cases are passing.Note: If adding site to the list of sites using across-tabs, please mention where to verify this in the PR description.
Parent-Child
communication. Will help in sending window-specific data to and fro apart from custom data messages.The MIT license (MIT)
Copyright (c) 2017-2019 Wingify Software Pvt. Ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
本文转自:http://jinaldesai.net/stop-sharing-session-state-between-multiple-tabs-of-browser/ Scenario: By default all browsers share session state between multiple tabs. So if you logged into one tab with
It’s often helpful to guide users through our apps as they navigate from screen to screen. A common technique to lead users through an app is to animate a Widget from one screen to the next. This crea
It’s often helpful to guide users through our apps as they navigate from screen to screen. A common technique to lead users through an app is to animate a Widget from one screen to the next. This crea
allows applications to maintain threadlocal-like state that follows execution as it moves to other execution contexts. The motivating examples are to eliminate the need for explicit async_callback wra
null null
问题内容: 我实现了带有ViewPager和TabsAdapter的ActionBarSherlock。它运作良好,但是现在我试图从另一个表中的“标签1”中加载的框架中“推送”。 该行为应类似于: 我的应用程序中有3个选项卡,启动时会看到第一个选项卡,其中有一个带有按钮的片段 按下第一个选项卡上的按钮,应替换第一个选项卡上的片段,并显示另一个片段 我试图实现这一点,但是在更换时有一个黑色的片段。
本文向大家介绍微信开发 js实现tabs选项卡效果,包括了微信开发 js实现tabs选项卡效果的使用技巧和注意事项,需要的朋友参考一下 最近微信应用号是炒的如火如荼,热门满满,但是也可以发现搜索关键词出来,各类网站出现的还都是微信的官方文档解释。正好赶上这个热潮,这几天先把小程序技术文档看了个遍,就直接着手写案例了。很多组件微信内部已经封装完了,正好发现没有tab选项卡效果,这两天正好研究了下。思