9

how to write javascript variable in iframe src?

Like

<iframe id="showskill" scrolling="yes" height="350" width ="350" src="http://localhost/POSkill/skillshow.aspx?user_id="+ ReturnURL() ></iframe>

Here ReturnURL() is a javascript function which returns a value. But the problem is in the iframe source I'm not getting the returned value of the function. Am I not putting in the right format or missing something?

Thanks in advance Johnny

Johnny
  • 1,555
  • 3
  • 14
  • 23
  • First step: Load up your html, right click, and select 'view source' then go down and find where the iframe tag exists. Check the 'src=' attribute, and please respond with what it says. If it has no value (or an incorrect value) for 'user_id=', it is likely an issue with your function, otherwise we will have to figure out another way to pass parameters. – BananaNeil Dec 27 '11 at 08:29

2 Answers2

23

You can't use JavaScript variables or functions directly within your html markup in that manner. What you can do is define your iframe first and then set its source from JavaScript:

<iframe id="showskill" scrolling="yes" height="350" width ="350" src=""></iframe>

<script>
    document.getElementById("showskill").src =
              "http://localhost/POSkill/skillshow.aspx?user_id="+ ReturnURL();
</script>

There are several other ways to achieve something similar, but I don't really want to go through them all when I'm not sure quite what your context is.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
4

You can not append the variable returned by function direcly as you did here. Do, something as given below.

var url=ReturnURL();
var urlPath='http://localhost/POSkill/skillshow.aspx?user_id='+url;
document.write('<iframe id="showskill" scrolling="yes" height="350" width ="350" src="'+urlPath+'><\/iframe>');
Umesh Patil
  • 10,475
  • 16
  • 52
  • 80
  • This is incorrect. Appending the return value of a function is perfectly possible. Try this: `var ohai = "O" + 'HAI'.toLowerCase();` ([jsfiddle here](http://jsfiddle.net/JScZh/)) – Peter-Paul van Gemerden Dec 27 '11 at 08:29
  • @PPvG - you can't append the return value _directly in html markup_ as shown in the question. I'm sure that's what Umesh meant, though the code sample in this answer then made that a bit ambiguous. – nnnnnn Dec 27 '11 at 08:32
  • @nnnnnn I think you might be right. If so: sorry for misunderstanding. – Peter-Paul van Gemerden Dec 27 '11 at 08:35
  • 1
    @PPvF, In your JS fiddle, it is perfectly accepted that variable can be appended to String returned by Function. but, speaking about iframe, where you want to display on HTML, we can not add the variable in the HTML tag. You need to render the iFrame with dynamic variable returned by function. I have used similar script many times before :) – Umesh Patil Dec 27 '11 at 08:38