0

I am new to JavaScript.

I have a javascript function which return me country name. Now I have to place this value in the src of iframe. My JavaScript function is as follows:

<script type="text/javascript">
var country = $("#SCountry").val();
function getCountry()
{
   var country = $("#SCountry").val();
   return country;

}
</script>

I am getting the country value but can't incorporate it in the src of iframe. My iframe src is as follows:

src='https://example.com/?country="javascript:getCountry()"'

It's not working.

halfer
  • 19,824
  • 17
  • 99
  • 186
Muhammad Maaz
  • 79
  • 1
  • 1
  • 15

5 Answers5

3

This will set the source attribute value of the iframe

<script type="text/javascript">
 var country = $("#SCountry").val();
 document.getElementById("iframeid").setAttribute("src","https://domain.com/country="+country)
 </script>
SarathSprakash
  • 4,614
  • 2
  • 19
  • 35
3

You cannot give javascript code in iframe src attribute directly.

Instead you can set src of iframe using Javascript like below

<iframe id="frame"></iframe>

<script>
    window.onload = function() {
        document.getElementById('frame').src = 'https://domain.com/?country=' + getCountry();
    }
</script>

In above approach we didn't set src attribute initially due to this IE will security warning. Better follow below approach

<iframe src='/images/spacer.png' onload="this.src = 'https://domain.com/?country=' + getCountry();"></iframe>

<!-- /images/spacer.png meant to be any file. Lesser size is better -->
M.G.Manikandan
  • 953
  • 5
  • 7
  • Thankssss to all.Its done now using the following code; – Muhammad Maaz Jun 24 '13 at 07:13
3

You cannot simply put JavaScript in an HTML attribute. It won't get evaluated. Instead you have to use JavaScript to manipulate the DOM element, i.e. change its src property.

<iframe id="myIframe" src=""></iframe>
<script>
    document.getElementById('myIframe').src = 
        'https://domain.com/?country=' + getCountry();
</script>
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1
$(function(jQuery){
 var aURL = $('#myIframe').attr( "src");
$('#myIframe').attr( "src",aURL+''+"Isreal"/*use getCountry() here*/);
});

May check this fiddle out

Ishank
  • 2,860
  • 32
  • 43
  • From the `javascript` tag info: *"Unless a tag for a framework/library is also included, a pure JavaScript answer is expected."* – Felix Kling Jun 21 '13 at 07:02
0

You can reach the "content" of your iFrame like this (just be sure to give an ID to the iFrame) This is a pure JS solution:

document.getElementById("iframe_id").contentDocument.getElementById("destination_id").innerHTML = country ;

And your iFrame tag could look like:
<iframe src='https://domain.com/' id="iframe_id">``</iframe>

OR

with php:

JS:

`document.getElementById('iframe_id').src = 'https://domain.com/?country=' + country;`

PHP inside iframe:

$country = $_POST["country"];
echo $country; // this echo you can use where you want the result to show up.
Sergio
  • 28,539
  • 11
  • 85
  • 132