0

I have a Javascript code on a popup window. Now I need to use it without pop up. It means in a page which consists of iframe.

Inside the Iframe, there is a page with the form name "ChatMsg" and text field name "mesg".

So I need to place onclick script on the main page, it should pass value into iframe page.

I added an ID for the iframe, iframe id is btnLocFrame

Current code in the top page are first two code parts given below, form is in third part of the code (source page of the iframe).

I tried to correct the code by changing window.opener into window.top but it doesn't work

<a href="javascript:pick(':IG');" alt=":IG" title=":IG"><img class="img-responsive" src="https://www.onlanka.com/chat/converts/smile7.gif"></a>   


<script language="javascript">
function pick(symbol) {
if(window.opener) window.opener.document.chatMsg.mesg.value += (window.opener.document.chatMsg.mesg.value == "") ? symbol : " " + symbol;
}
</script>
<form name="chatMsg">
<input type="text" name="mesg" id="mesg" maxlength="512" value="" autocomplete="Off">
</form>

I had expected to pass assigned value of the link into form text field after click on the link but it doesn't work.

Please help me to correct the code

  • 1
    This answer might help you https://stackoverflow.com/questions/536538/pass-value-to-iframe-from-a-window – N.P. Jan 18 '19 at 08:21
  • 1
    Please explain your problem you wanna fix – Googlian Jan 18 '19 at 08:24
  • If the link and the function are in your main document, then what do you want with `window.top` …? Your main document doesn’t have another one “above” it. (But it makes no sense that this would have used `window.opener` then in the first place.) – misorude Jan 18 '19 at 08:29
  • Can you share the complete code please? It will be even better if you provide the code on jsfiddle or codepen. – Prashant Zombade Jan 18 '19 at 09:12
  • @PrashantZombade Visit https://www.onlanka.com/mychat/js-modal/ then click on "Open Modal" button, then click on an emoji, its not passing assigned value into form field – Udaya Arunakantha Jan 18 '19 at 09:18
  • @Googlian Visit https://www.onlanka.com/mychat/js-modal then click on "Open Modal" button, then click on an emoji, its not passing assigned value into form field – Udaya Arunakantha Jan 18 '19 at 09:22
  • In the given link in which page have you written the `pick()` function? – Prashant Zombade Jan 18 '19 at 09:25
  • @PrashantZombade /js-modal/index.html has `pick()` function, if you view source ctrl+u you'll see. inside the bottom iframe, there is button.html which has "Open modal" button. – Udaya Arunakantha Jan 18 '19 at 09:28

1 Answers1

1

As you are trying to get Imoji code in to a Iframe element using the modal try to change your pick function as below

function pick(symbol) {

  var iframe = document.getElementById('btnLocFrame');
  var innerDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;

  var oldValue = innerDoc.getElementById("mesg").value;

  innerDoc.getElementById("mesg").value = oldValue + symbol;

  //if (window.opener) window.opener.document.chatMsg.mesg.value += (window.opener.document.chatMsg.mesg.value == "") ? symbol : " " + symbol;
}
Googlian
  • 6,077
  • 3
  • 38
  • 44