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.

6
  • where in the iFrame do you want it? any special div? how does the iFrame html look like? Commented Jun 21, 2013 at 6:50
  • Have you update jquery src link? Commented Jun 21, 2013 at 6:51
  • i want the javascript function value in query string in src of iframe Commented Jun 21, 2013 at 6:56
  • iFrame will get loaded only if all the dependent elements loaded (probably onload of body) fully. So check whether your getCountry() method is called and returning the correct value when iFrame gets loaded Commented Jun 21, 2013 at 6:57
  • @AyyappanSekar: I can already tell you that it doesn't since JavaScript is not evaluated in HTML attributes. Commented Jun 21, 2013 at 7:01

5 Answers 5

4

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 -->
Sign up to request clarification or add additional context in comments.

1 Comment

Thankssss to all.Its done now using the following code;<iframe id="frame"></iframe> <script> window.onload = function() { document.getElementById('frame').src = 'domain.com/?country=' + getCountry(); } </script>
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>

Comments

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>

Comments

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.

Comments

0
$(function(jQuery){
 var aURL = $('#myIframe').attr( "src");
$('#myIframe').attr( "src",aURL+''+"Isreal"/*use getCountry() here*/);
});

May check this fiddle out

1 Comment

From the javascript tag info: "Unless a tag for a framework/library is also included, a pure JavaScript answer is expected."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.