0

I need to output a DIV width into a URL for an iframe but am having some trouble. I have managed to get java to output the div width, but encounter a problem when getting this into the URL. Below is the code I am using (notice the width=

<iframe src="http://www.coveritlive.com/index2.php/option=com_altcaster/task=viewaltcast/altcast_code=3f43697a78/height=670/width=<script language='javascript'>var e = document.getElementById('Single2');
document.write(e.offsetWidth);</script>"></iframe>

This outputs the URL as:

http://www.coveritlive.com/index2.php/option=com_altcaster/task=viewaltcast/altcast_code=3f43697a78/height=670/width=var e = document.getElementById('Single2'); document.write(e.offsetWidth);

As you can see the URL has the full javascript in, not just it's output.

Ideally the URL should be as such (lets assume the DIV width is 650px).

http://www.coveritlive.com/index2.php/option=com_altcaster/task=viewaltcast/altcast_code=3f43697a78/height=670/width=650

Any ideas how I can get this working?

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Java and JavaScript are totally different. – Pointy Jan 01 '12 at 14:46
  • 1
    `Java != Javascript`. See [Java vs. JavaScript: Similarities and Differences](http://www.htmlgoodies.com/beyond/javascript/article.php/3470971/Java-vs-JavaScript.htm), [Java vs. JavaScript](http://www.dannyg.com/ref/javavsjavascript.html), and [What is JavaScript and how is it different from Java Technology?](http://www.java.com/en/download/faq/java_javascript.xml). – Jomoos Jan 01 '12 at 14:47
  • Apologies. As you can see total novice here. – Kevin Robinson Jan 01 '12 at 14:48
  • I don't know why you got a low grade on the question. This is a relevant question. I've seen many developers making this mistake.. – guy mograbi Jan 01 '12 at 15:03

2 Answers2

3

You should do this in the following way (pseudo code)

<iframe id="myIframe"></iframe>
<script>
document.getElementById("myIframe").src = ... // construct URL here 
</script>

Let me know if you need a working example.

Here is a working example

<head>
<script type="text/javascript">
function changeContent()
{
    console.log("changing src");
    var myIframe = document.getElementById("guy");
    myIframe.src = "http://steps.mograbi.info/users/sign_in?unauthenticated=true&width=" + myIframe.offsetWidth;
}

</script>
</head>
<body>
<iframe id="guy"></iframe>
<script>
document.onload = changeContent();
</script>
</body>

If you track the network, you will see the width passing.. Network as seen in chrome when running my example

guy mograbi
  • 27,391
  • 16
  • 83
  • 122
1

You can't put <script> tag in src, it will be treated as String.

<iframe id="myiframe"></iframe>

<script type='text/javascript'>
var e = document.getElementById('Single2');
var url = "http://www.coveritlive.com/index2.php/option=com_altcaster/task=viewaltcast/altcast_code=3f43697a78/height=670/width=" + e.offsetWidth;
document.getElementById("myiframe").setAttribute("src",url);
</script>
ijse
  • 2,998
  • 1
  • 19
  • 25