Search code examples
javascriptjquerywindow.openwindow.opener

New window not getting the value on click [window. opener.]


I am trying to open a new window on click and send the current index value to the new window.

Here is my HTML where I want to change the value on click, default value is 1

<input type="text" id='pageno' value="1">

Here is my jQuery code.

$('.page-list li').click(function() {
    var pageIndex = $(this).index() + 1;
    var URL = window.location.href;
    var myWindow = window.open(URL);
    myWindow.opener.document.getElementById('pageno').value = pageIndex;
});

Solution

  • I wouldn't try to access the other window like that. What you can do is add the information to the url. Either using a hash #1 or a query string ?pageno=1. Since you don't want to use any server-side languages I would go for a hash since that's easier to work with using JavaScript.

    $('.page-list li').click(function() {
       var pageIndex = $(this).index() + 1;
       var URL = window.location.href + '#' + pageIndex;
       var myWindow = window.open(URL);
    });
    

    Then at the opened window you can acquire the page number:

    var pageno = window.location.hash.substr(1);