-4

Passing a javascript variable using tag in iframe by attaching variable like this "+var+" but its not passing value to next page . is there any different way or any bug in my code please suggest.

here is my code.

    <script>
    var user = "user@user.com"; 
    </script>

 <iframe src="http://127.0.0.1/ve2/ve2/www/kitchen-sink/api/fm/df.php?user="+user+""></iframe>

Thanks in advance .

  • All your code is doing is defining a variable, include all relevant code – Patrick Evans Mar 28 '18 at 11:21
  • check it now @PatrickEvans – uttam sharma Mar 28 '18 at 11:22
  • @uttamsharma this is not how Javascript works. You have to change the src attribute of the iframe via javascript. See https://www.w3schools.com/jsref/met_element_setattribute.asp for more information on how to set attributes. – BRO_THOM Mar 28 '18 at 11:22
  • That iframe part isn't in javascript `+user` there is meaningless – Patrick Evans Mar 28 '18 at 11:23
  • so can you please suggest me how can i open a url in a iframe and pass a javascript variable value to next page @BRO_THOM – uttam sharma Mar 28 '18 at 11:24
  • Hi Uttam. Welcome to SO. Don't be disheartened by the downvotes on your question - it's just a way for the community to keep the content on this page organised, clean and useful. To help us help you better please read the [How to Ask](https://stackoverflow.com/help/how-to-ask) guide and rework your question to include the proper context, a [Minimum Complete Verifiable Example](https://stackoverflow.com/help/mcve) and a clear question. – Chirag Ravindra Mar 28 '18 at 11:24
  • so can you please suggest me how can i open a url in a iframe and pass a javascript variable value to next page @PatrickEvans – uttam sharma Mar 28 '18 at 11:25
  • @uttamsharma I've included an answer for this situation. – BRO_THOM Mar 28 '18 at 11:25

1 Answers1

0

You cannot execute javascript directly, it has to be executed from script tags OR via an event.

<iframe id="iframe"></iframe>
<script>
    var iframe = document.getElementById('iframe');
    var user = "user@user.com";
    iframe.setAttribute('src', "http://127.0.0.1/ve2/ve2/www/kitchen-sink/api/fm/df.php?user=" + user );
</script>
BRO_THOM
  • 824
  • 9
  • 24