How to avoid javascript to open new window blocked by browser

  After my personal test, I can solve it perfectly by using the virtual form. The core meaning of this method is that the browser does not block the window.open() action of JavaScript in the current window.
  Using this rule, open a blank window with the specified title in the current window, and then submit the virtual form to the window corresponding to the title.

  1. The wallimn-util.js file. A separate general js file, put many general functions
/**
 * JS submits data via POST via virtual form
 * @param URL
 * @param PARAMS
 * @returns
 */
function jsPost(URL, PARAMS, TARGET) {
	  var temp = document.createElement("form");
	  temp.action = URL;
	  temp.method = "post";
	  temp.style.display = "none";
	  if(!TARGET){
		  TARGET="_self";
	  }
	  temp.target=TARGET;
	  for (var x in PARAMS) {
	    var opt = document.createElement("textarea");
	    opt.name = x;
	    opt.value = PARAMS[x];
	    temp.appendChild(opt);
	  }
	  document.body.appendChild(temp);
	  temp.submit();
	  return temp;
	}


  2. The page that needs to open the window with javascript:
//Some people say that this function needs to be called through the onclick of a. I haven't tested the button to see if it works.
//If you want to put window.open not on the current page and write it in the wallimn-util.js file above, the browser will also block it.
function jsOpenWin(){
  var title="js new window";
  window.open("about:blank",title);
  jsPost("wallimn.iteye.com",{name:"wallimn"},title);
}


  3. Important note:
   window.open can only be written in the current page.
   window.open can only be written in the current page.
   window.open can only be written in the current page.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326682016&siteId=291194637