0

This is what I have.

URL = abc.com/?em=xyz&fn=123

I have an iframe on the page which I want to share some of the param data as follows...

iframe= def.com/xyz

As you can see I just want one of the url params from the main source url to carry across to the iframe, to be part of the url, not an added param on the iframe string. It would always be the single param 'em' that would be carried across, all other params would be ignored.

I think this was clear, but just to show an example of correct iframe = def.com/xyz and wrong would be an iframe with the url = def.com/?em=xyz. I know the latter seems possible in Javascript. I just cannot work out the former. Thanks

Hope someone has any help.

The site is currently on Wordpress if that makes a difference. The iframe url is an external link,not wordpress

Thanks

7
  • I think this was clear, but just to show an example of right = def.com/xyz and wrong would be an iframe with the url = def.com/?em=xyz. I know the latter seems possible in Javascript. I just cannot work out the former. Thanks
    – Chris
    Commented Apr 16, 2017 at 9:04
  • What you want is clear, how you want to achieve it is not. Is this going to be coded in PHP? What have you tried? Commented Apr 16, 2017 at 9:06
  • Yes happy to do this in PHP or whatever works! I had an issue with the javascript option which I found here as it passes it as a param rather than part of the url.
    – Chris
    Commented Apr 16, 2017 at 9:13
  • I think you have to give us a starting point... variables to handle... we cannot guess code out of thin air. How will you integrate the PHP in your code? Commented Apr 16, 2017 at 9:19
  • Hmm, It looks like even knowing how to word my question is out of my depth then. I'm not sure how else I can word the example. I was just looking to pass a url param (em) into the url of an embeded iframe url.com/(em) . If there was any other way to word it I would happily add more info. There does seem plenty of chat about Javascript ways to do this if I want to pass it as url.com/?em=(em) but not the former. What can I give you to help?
    – Chris
    Commented Apr 16, 2017 at 9:27

2 Answers 2

1

Right, I have a solution that is working for me so thought I would share. It is important to note that this will probably only work if you are using Wordpress...

Step 1. I created a new page template , called page-iframe php. which references a content file called content-iframe php

In this file I created the iframe code..

<iframe src="domain.com/<?php echo do_shortcode('[urlparam param="em" /]') ?></iframe>

This uses the URL Params Wordpress plugin to read the url and place the param of choice into the iframe which is hard coded into the page template, rather than added in the content/edit area of the wordpress back end.

The only drawback to this as I see it will mean a new page template for every domain you want to use inside the iframe. I only require one domain to be referenced so this is a solution for me.

0

Purely javascript:

First a function to grab the parameters in the parent URL:

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];}
       }
       return(false);
}

Next, call the function to get the 'em' parameter value and store as a variable. Also check that it is defined and not erroneous.

var myParam = getQueryVariable("em");
if ((typeof myParam !== "undefined") && (myParam !== false)) {

Next, create your iframe URL:

    var iframeURL = "def.com/".concat(myParam);

Next, assign the iframe URL in your html to this new iframeURL:

    document.getElementById('iFrameName').src = iframeURL;
    }

Optional; sending the url without an em parameter. You could have done this already in your html.

else{
    document.getElementById('iFrameName').src = "http://def.com/";
}

All together:

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];}
       }
       return(false);
}

var myParam = getQueryVariable("em");
if ((typeof myParam !== "undefined") && (myParam !== false)) {
    var iframeURL = "def.com/".concat(myParam);
    document.getElementById('iFrameName').src = iframeURL;
    }
else{
    document.getElementById('iFrameName').src = "http://def.com/";
    }
1
  • This is encouraging. Thanks . I've not got it working yet, but I think we are making progress. I will keep playing on this later and report back.
    – Chris
    Commented Apr 16, 2017 at 10:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.