Cannot set Property 'innerHTML' of null "ERROR"

Asked

Viewed 585 times

-1

Hello, I’m trying to open an external site through a page I created by clicking with a button, but I can not reference the document I created, follow the code below, tried several times and could not, any help is valid, thank you very much.

<!DOCTYPE html>
<head>
	<title>Principal</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
	<meta name="viewport" content="width=device-width, initial-scale=1"/>
	
	<script src="\\fswcorp\ceic\ssoa\gaacc\System\JQuery\jquery-3.2.1.min.js"></script>
	<script src="\\fswcorp\ceic\ssoa\gaacc\System\jQueryMask\dist\jquery.mask.min.js"></script>
	<script src="\\fswcorp\ceic\ssoa\gaacc\System\jQueryUI\jquery-ui.js"></script>
	<script>

	$(document).ready(function(){
		$("#dateBegin").mask('00/00/0000');
		$("#dateEnd").mask('00/00/0000');
	
		$("#buttonDownloadBRScan").click(function(){
			$windowopen = window.open();
			$windowopen.location.href = "https://www.fdibr.com.br/autenticacao/autenticacao/login";
			$test = $windowopen.document.getElementById("usuario").innerHTML = "7478704";
		})
	});
			
	</script>
</head>

<body>
	<div class="dataInput">
		<label id="labelDateBegin">Data Inicial</label>
		<input id="dateBegin" type="date"/>
		<label id="labelDateEnd">Data Final</label>
		<input id="dateEnd" type="date"/>
	</div>
	<br><br>
	<button id="buttonDownload">Download</button>
	<button id="buttonDownloadBRScan">Download BRScan</button>
</body>

  • document.getElementById("usuario").innerHTML - I don’t see any tag with id usuario in html

  • you have control over the page that is being opened in window.open and it is of the same domain? If not, you can forget about it.

  • this html that is on my page, I am automating the process of loading another page and automatically fill the user id of the other page that is an external link. but I am not able to manipulate the element of the other page through my javascript.

  • @Felipediasdesouza, it would not be the case to store this value in a cookie?

  • the problem is not storing the variable but getting control of $windowopen, since it dies when the click() ends, and after the click() end I cannot work with this variable again because its scope is over.

1 answer

1

You need to include a onload to know when the page opened with window.open was loaded and so change its element. But it is also necessary to pass some parameter to window.open. You can put null.

For once, it doesn’t work on IE.

Change the part of your code to the below:

$(document).ready(function(){
   $("#dateBegin").mask('00/00/0000');
   $("#dateEnd").mask('00/00/0000');
   $("#buttonDownloadBRScan").click(function(){
      $windowopen = window.open(null);
      $windowopen.location.href = "https://www.fdibr.com.br/autenticacao/autenticacao/login";

      $($windowopen).on("load", function(){
         $($windowopen.document)
         .find("#usuario")
         .html("7478704");
      });
   });
});

Functional example

  • realized your idea, however I can not enter the information in the user element that is in the other page, it loads the page, but does not assign the value in the element.=/

  • The only way then would be to pass the information via URL. For this it is necessary to include a code also in the open page.

  • I cannot pass information via URL because the page is not mine, it is from a third party.

  • I think there is no way, the browser blocks access to the content of the third party page.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.