I have created a web page with Javascript code that dynamically creates a new page. The problem I have is that the Javascript functions are not "seen" by the created page, so they cannot be called. It works perfect in Chrome and Safari browsers, in which it seems the context (known variables and functions) is fully passed to the created page, whereas it fails in Microsoft Edge and Internet Explorer My project is quite complex but I could isolate my problem in the simple code sample that creates a web page with a PLUS and a MINUS button to incremement/decrement an index value:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>JS Tester</title>
</head>
<script type="text/javascript">
var index = 5;
$(document).ready(function() {
callPage();
});
function callPage() {
document.open();
document.write('<!DOCTYPE html>');
document.write('<html>');
document.write('<body>');
document.write('<button id="DecButton" onclick="decrementIndex()" style="height: 30px; width: 50px"> - </button>');
document.write('<p id="indexVal">' + index + '</p>');
document.write('<button id="IncButton" onclick="incrementIndex()" style="height: 30px; width: 50px"> + </button>');
document.write('<br>');
document.write('</body>');
document.write('</html>');
document.close();
document.title = 'Generated Page';
}
function decrementIndex() {
index--;
document.getElementById("indexVal").innerHTML = index;
}
function incrementIndex() {
index++;
document.getElementById("indexVal").innerHTML = index;
}
</script>
<body>
Hello World
</body>
</html>
It is also available here: http://emarciano.free.fr/js/tester.html
In Edge browser, clicking on a button causes an error like: 'incrementIndex' is not defined This is probably because the function is unknown from the page generated using document.open() I feel frustrated because it works perfect in Chrome. Is there a way to have it working the same way in Edge?
BTW, I found strange that some examples from W3schools.com do not work as expected in Edge, examples coming from the documentation of the document.open() function:
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_doc_write5
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_doc_open
Thanks for your help,
Eric