I have a source HTML page from which I open a virtual (not bound to a file) pop-up by clicking a generic button and by which I push some Html and Javascript code in its innerHTML
.
This is a short simplified extract of the Source Page in which I define the Html+Javascript content of the pop-up and open it:
<script language="javascript">
<!--
function fxOpenWindow()
{
// Init objects
var retVal = ''; // contains the Html+Javascript to push in the pop-up innerHTML
var popup = ''; // pop-up to open, not generated by loading a file but only by pushing the "retVal" code inside its innerHTML
// constructing the HTML to push
// defining a function() to show a simple alert : "1"
retVal = retVal + '<script>';
retVal = retVal + ' function fxMessage() { alert(1); }';
retVal = retVal + '</script>';
// defining the html object with which execute the above function
retVal = retVal + '<button onClick="fxMessage();">CLICK ME</button><br>';
// opening the virtual POPUP
popup = window.open('','title');
popup.document.body.innerHTML = retVal;
popup.focus();
}
-->
</script>
<button onClick="fxOpenWindow();">OPEN THE POP-UP WINDOW AND SHOW ITS CONTENT</button>
My problem is that I can't make the fxMessage()
function work, it just doesn't do anything at all.
I have used the eval()
method too, trying to make the string become live executable code:
// all these methods are a "NO-GO"
retVal = retVal + '<button onClick="fxMessage();">CLICK ME</button><br>';
retVal = retVal + '<button onClick="eval(fxMessage);">CLICK ME</button><br>';
retVal = retVal + '<button onClick="eval(fxMessage());">CLICK ME</button><br>';
// the "direct method" is the only one that works:
retVal = retVal + '<button onClick="alert(1);">CLICK ME</button><br>';
Since the real code is more complex, I have the need to use a function() for simplifying some recursively calls I have to write in order to dynamically build more html objects in the popup itself, so I DO really mind to write all the code inside the onClick
event; another thing I'm not willing to do is to have the pop-up load a html file stored on the hard-disk in which I have wrote all the retVal
as its html in order to be able to have the fxMessage()
function working correctly: the pop-up must be just a virtual page resident only in memory the time is open.
My question is : is there a way to have the fxMessage()
to be elevated a as function in the pop-up and make it work out its job?