I use the following JavaScript to open new window in which Joomla login page displayed:
window.open('https://URL', '_blank', 'location=yes');
The new window opens is as follows:
I need to populate the username in its field automatically. I found postMessage() method very suitable to do cross-domain communications (e.g., this example) . So I amended my JavaScript like this:
var loginWindow = window.open('https://URL', '_blank', 'location=yes');
loginWindow.postMessage('username', 'foobar');
On the receiving side, (Joomla), I need to add this code in order to catch the post message:
window.addEventListener('message', function(event) {
var message = event.data;
$('input.login').val(message);
});
My question is, where can I add this code?
I'm not familiar with Joomla structure, and I did not see any JavaScript in the files like login form (components/com_users/views/login/tmpl/default_login.php). So where can I add this JavaScript code. If I cannot, what alternatives do I have?
Update
Based on @Rene Korss advice, I added my code at the end of the following file /templates/theme718/html/com_users/login/default_login.php
I mention part of the previous code in the file to show its nature. My code starts from <?php
<script>
......
if(jQuery( window ).height()<620){
jQuery('#copy-rightfp').addClass("copy-rightfps");
} else {
jQuery('#copy-rightfp').removeClass("copy-rightfps");
}
function alertforget(){
var div = '<div class="green"><p><?php echo JText::_('COM_FORGET_OOPS') ?></p></div><div class="font12"><p><?php echo JText::_('COM_FORGET_PLEASE_CALL') ?></p></div>';
jQuery('#msgcontainer .note').html('');
jQuery('#msgcontainer .note').append(div);
jQuery('#msgcontainer').bPopup({
modalClose: false,
opacity: 0,
positionStyle: 'fixed'
});
}
<?php
$document = JFactory::getDocument();
$document->addScriptDeclaration("
window.addEventListener('message', function(event) {
var message = event.data;
$('input#username').val(message);
});
");
?>
</script>
I find that the item I want to populate the message into called username
, from below:
However, when the window opens, nothing occurs. No errors show in the console