Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9219857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:12:45+00:00 2026-06-18T03:12:45+00:00

I need to have a function execute on a page in one tab when

  • 0

I need to have a function execute on a page in one tab when certain conditions are met in another tab. All I need to do is send some kind of nudge to the other tab. I’ve tried many things already in conjunction with a timer to keep polling:

  • GM_setValue (not supported in Chrome apparently)
  • Setting top.item (apparently doesn’t work between tabs)
  • Cookies (even though my userscripts are running in two tabs on the same domain, this doesn’t seem to work)

Any other ideas? And yes, I do need to use Chrome, even though it seems intent on thwarting me in this >.>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:12:46+00:00Added an answer on June 18, 2026 at 3:12 am

    Since the tabs are on the same domain, you can use localStorage.

    1. Set the script to run on both pages, EG:

      // @include  http://YOUR_SERVER.COM/YOUR_PATH/pitcher/*
      // @include  http://YOUR_SERVER.COM/YOUR_PATH/batter/*
      
    2. Be sure you can tell which page is which. By the URL, for example, or some different content.

    3. The sending page merely sets values as desired, EG:

      localStorage.setItem ('targetAddress', 'http://puppies.com/');
      
    4. The receiving page listens for storage events like:

      $(window).bind ("storage", function (zEvent) {
          ...
      } );
      

      or

      window.addEventListener ("storage", function (zEvent) {
          ...
      }, false);
      

    Putting it all together, here is a complete script, that works in both Firefox and Chrome (and probably Opera and others).

    You can test it against this “sender” page, and this “receiver” page.

    // ==UserScript==
    // @name     _Cross tab, same domain communication
    // @include  http://jsbin.com/ihoboz/*pitcher*
    // @include  http://jsbin.com/ihoboz/*batter*
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/pitcher/*
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/batter/*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
    // @grant    GM_addStyle
    // ==/UserScript==
    /*- The @grant directive is needed to work around a design change
        introduced in Greasemonkey 1.0.   It restores the sandbox.
    */
    
    function GM_main ($) {
        /*-- Is this the sending or receiving page?
            In our example, the sender has "pitcher" in the URL,
            while the receiver has "batter" in the URL
        */
        var isSender = false, isReceiver = false;
    
        if (/pitcher/i.test (location.href) ) {
            isSender    = true;
        }
        else if (/batter/i.test (location.href) ) {
            isReceiver  = true;
        }
    
        if (isSender) {
            //-- Add 2 buttons to change the data we send to the other tab.
            $("body").prepend (
                '<button class="gmTestButtons">Set the transmitted value.</button>' +
                '<button class="gmTestButtons">Reset the transmitted value.</button>'
            );
    
            $("button.gmTestButtons").click ( function () {
    
                if (/^Set the transmitted/.test (this.textContent) ) {
                    localStorage.setItem ('targetAddress', 'http://puppies.com/');
                }
                else {
                    localStorage.setItem ('targetAddress', 'http://unicorns.com/');
                }
            } );
        }
        else if (isReceiver) {
            //-- Listen for changes in local storage
            $(window).bind ("storage", function (zEvent) {
                var varName     = zEvent.originalEvent.key;
                var newValue    = zEvent.originalEvent.newValue;
    
                alert (
                    'Received new variable, "'   + varName
                    + '", with a new value of: ' + newValue
                );
            } );
        }
    }
    
    //-- Style and/or postion our buttons
    GM_addStyle ( "                                 \
        button.gmTestButtons {                      \
            margin:                 1em;            \
            padding:                1ex 1em;        \
            font-size:              20px;           \
            background:             pink;           \
        }                                           \
    " );
    
    
    //--- The rest of this just loads jQuery in a cross-browser way.
    //
    if (typeof jQuery === "function") {
        console.log ("Running with local copy of jQuery!");
        GM_main (jQuery);
    }
    else {
        console.log ("fetching jQuery from some 3rd-party server.");
        add_jQuery (GM_main, "1.7.2");
    }
    
    function add_jQuery (callbackFn, jqVersion) {
        var jqVersion   = jqVersion || "1.7.2";
        var D           = document;
        var targ        = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
        var scriptNode  = D.createElement ('script');
        scriptNode.src  = 'http://ajax.googleapis.com/ajax/libs/jquery/'
                        + jqVersion
                        + '/jquery.min.js'
                        ;
        scriptNode.addEventListener ("load", function () {
            var scriptNode          = D.createElement ("script");
            scriptNode.textContent  =
                'var gm_jQuery  = jQuery.noConflict (true);\n'
                + '(' + callbackFn.toString () + ')(gm_jQuery);'
            ;
            targ.appendChild (scriptNode);
        }, false);
        targ.appendChild (scriptNode);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two function and I need to execut in one onClick . The
I have some function where I need to pass a point datatype . somefunc(United
I have any number of anchor links on a page that need to execute
I need to load a web page, execute its JavaScript (and all js files
When I have some code I need to execute more than once I wrap
I need to make some JQuery execute when the page/document has changed - in
I am busy writing my own collection type and need to have a function
I have function LoadTempMovieList(), and need to load movies from sessionStorage. But it seems
I have a function that i need to call on iframe mousemove(). But i
I have need for a function pointer that takes two arguments and returns a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.