PDA

View Full Version : Get Url value and load in iframe



hannah sofia
12-12-2011, 06:17 PM
Hello,
In php you can have a url formatted with http://url.com?id=123456 to load inside an iframe:


<iframe src="index.php?id=<?php echo $_GET["id"]; ?>" frameborder="1"></iframe>

and have an outcome:


<iframe src="index.php?id=123456" frameborder="1"></iframe>

What is the js version of this code?
Hope you can help me :)

hannah sofia
12-12-2011, 06:26 PM
I tried <script language="javascript" type="text/javascript">document.write (document.location.href);</script> but it's not working.. :(


<iframe width="400" height="400px" src="index.php?id=<script language="javascript" type="text/javascript">document.write (document.location.href);</script>" frameborder="1"></iframe>

I only need 123456 and not to get the full url from the current browser...

hannah sofia
12-12-2011, 07:18 PM
I am getting close!


<script>
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
</script>

then


index.php?id=<script type='text/javascript'>document.write( getQueryVariable("id") );</script>

if I type myurl.com/sample.html?id=123456
it will show me
index.php?id=123456

But how can I make index.php?id=123456 as the iframe src?


<iframe src="index.html?id=<script type='text/javascript'>document.write( getQueryVariable("x") );</script>"></iframe>

is not working... :(

jscheuer1
12-12-2011, 07:54 PM
You can't have a script tag inside a tag. You have to write out or create that tag. So something like so should work (untested):


<script type="text/javascript">
if(getQueryVariable("id")){
document.write('<iframe src="index.html?id=' + getQueryVariable("id") + '"></iframe>');
} else {
document.write('<iframe src="index.html"></iframe>');
}
</script>
<noscript>
<iframe src="index.html"></iframe>
</noscript>

hannah sofia
12-12-2011, 08:25 PM
Thanks for ur reply :)
I tried but not working... :(

hannah sofia
12-13-2011, 12:39 AM
Hi Sir, pls make it work for me... I'm willing to donate.
I already spent 8 hrs in google but can't find solution...
I only knew copy/paste :(

jscheuer1
12-13-2011, 12:45 AM
Sorry typo, should be (also corrected in my previous post, I said it wasn't tested):


<script type="text/javascript">
if(getQueryVariable("id")){
document.write('<iframe src="index.html?id=' + getQueryVariable("id") + '"></iframe>');
} else {
document.write('<iframe src="index.html"></iframe>');
}
</script>
<noscript>
<iframe src="index.html"></iframe>
</noscript>

But why would you want to do this? What's happening on index.html that you need javascript to supply it with a query string?

Oh, and by the way, this line in the getQueryVariable() function:


alert('Query Variable ' + variable + ' not found');

For production, that line should probably be:


return null;

hannah sofia
12-14-2011, 05:12 AM
Thanks John for ur help, it's working now :)
I am using it on my blogger site. Thanks again :)