Search code examples
javascriptwindow.openwindow.opener

Javascript window.opener not working in asp.net mvc-5


I am creating a web app in asp.net mvc in which I am redirecting from one view to another using window.open

here is my code in javascript

window.open('@Url.Action("Details", "Details"), "_self");

I have some variable in JS which I declared in script Tag like the following

var1 = "0";
var2 = "2";
var3 = "0";
var4 = "0";

but after redirecting I am not able to get the values of these variable

when I type console.log(window.parent.var2);

I am expecting to get 2 but actually I am getting undefined

what I need to do, to use Javascript's window.opener in asp.net mvc


Solution

  • If you change this

    window.open('@Url.Action("Details", "Details"), "_self");
    

    to

    window.open('@Url.Action("Details", "Details"), "_blank");
    

    it would work but you would have two windows open. window.opener and window.parent only works if both the opener and opened window/page are both open. Neither work on a _self link.