1

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?

4

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.