简体   繁体   中英

How to call a Java script function after my app is open in a new tab?

I have a kind of single page application, and in one scenario I need to open my app in a second tab, and the second tab must be open with a specific partial view.

I have this function to open the app in a new tab:

function OpenInNewTab(id) {

    var win = window.open("Index","_blank");
    win.focus();
  //MainMenuClick("CandidateInfo", id) <-- this function I want to call on the new Tab, after loading. 
}

OpenInNewTab is opening my app in a new tab on the "Home/Index" page, which is ok (half of the task);

Now I want to call MainMenuClick in order to open a specific partial view, inside Index page (in the new tab).

I do not know how to call a function in the new open tab!

Is it possible to tell to the browser to call " MainMenuClick " function in the new tab?

You can use query string to achieve that, take current partial view id and pass it through window.open like this:

var win = window.open("Index?partialViewId=" + partialViewId,"_blank");

In your SPA global script, search url for that specific key and if it exists call MainMenuClick with the value you've got.this means when the page is loading if a key with the name partialViewId exists, script should select that partial view after page loaded. in order to do that use this function to get any key from url:

function getParameterByName(name, url) {
   if (!url) url = window.location.href;
   name = name.replace(/[\[\]]/g, "\\$&");
   var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
       results = regex.exec(url);
   if (!results) return null;
   if (!results[2]) return '';
   return decodeURIComponent(results[2].replace(/\+/g, " "));
}

and finally get your id like this:

var partialViewId = getParameterByName('partialViewId');
if(partialViewId != null)
    MainMenuClick("CandidateInfo", partialViewId);

hope that helps.

I think a feasible solution, in your case it to put the call to "MainMenuClick" just after the opening of the window.

    function OpenInNewTab(id) {
        var win = window.open("Index","_blank");
        var myVar=setInterval(function(){myTimer(win,id)},1000);
    }
    function myTimer(win,id){
        win.MainMenuClick("CandidateInfo", id);
        win.focus();
    }

Note that the MainMenuClick function is called after one second; because we want to prevent javascript from making a call in the context of a page that has not been properly initialized.

For other less empirical solutions you can use a server side script to distinguish between requests that one the first page from the others that want the second, using some POST or GET variables; and use the onload event of the body of the new page to execute whatever code you want.

You may also be interested in the answers posted here -> Javascript communication between browser tabs/windows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM