Results 1 to 8 of 8

Thread: Get Url value and load in iframe

  1. #1
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Get Url value and load in iframe

    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

  2. #2
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    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...
    Last edited by hannah sofia; 12-12-2011 at 06:37 PM.

  3. #3
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    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...

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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):

    Code:
    <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>
    Last edited by jscheuer1; 12-13-2011 at 12:37 AM. Reason: fix typo in code
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks for ur reply
    I tried but not working...

  6. #6
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    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

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Sorry typo, should be (also corrected in my previous post, I said it wasn't tested):

    Code:
    <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:

    Code:
    alert('Query Variable ' + variable + ' not found');
    For production, that line should probably be:

    Code:
    return null;
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. The Following User Says Thank You to jscheuer1 For This Useful Post:

    hannah sofia (12-14-2011)

  9. #8
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks John for ur help, it's working now
    I am using it on my blogger site. Thanks again

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •