A new window does not receive a value when [window. knife.]

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

Here is my HTML where I want to change the value on click, default value 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;
});
+4
source share
2 answers

I would not try to access another window. What you can do is add information to url. Either use a hash #1or a query string ?pageno=1. Since you do not want to use any server languages, I would go for a hash, as this made it easier to use JavaScript.

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

:

var pageno = window.location.hash.substr(1);
+3
window.location = "YOUR_URL?pageIndex="+pageIndex

and on newly opened window write below code in onload() method 

function onload(){
 var pageIndex = getParameterByName('pageIndex');
 document.getElementById('pageno').value = pageIndex;
}

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, " "));
} 
0

Source: https://habr.com/ru/post/1670163/


All Articles