0
<script>
            /* source parameters */
                    function GetURLParameter(sParam)
                    {
                        var sPageURL = window.location.search.substring(1);
                        var sURLVariables = sPageURL.split('&');
                        for (var i = 0; i < sURLVariables.length; i++)
                        {
                            var sParameterName = sURLVariables[i].split('=');
                            if (sParameterName[0] == sParam)
                            {
                                return sParameterName[1];
                            }
                        }
                    }
                    var mysrc = GetURLParameter("externalsrc");
                    if(mysrc == null) {
                        mysrc='another_site';
                        }
                    var objHidden = document.getElementById("externalsrc");
                    objHidden.value = mysrc;
</script>

Before, I used to use the above code when I hard coded the form code directly in the HTML page of my site. The above code easily checks url parameters from the address bar and gives the value of externalsrc to a form field's value. Now, I created a portable iframe widget that can be used elsewhere externally. I'd like to update the value of <input type="hidden" externalsrc="" value"" /> from the iframe's extsrc argument.

Here's an example of the iframe code:

<iframe src="http://www.example.com/joinuswidget.html?externalsrc=mywidget" marginheight="0" marginwidth="0" frameborder="0" height="350" scrolling="no" width="190"></iframe>

I'd like my form's externalsrc's value to be "mywidget" taken directly from the iframe's source. Is it possible?

Thanks,

tleo
  • 351
  • 3
  • 16
  • Instead of reading the window.location, you want to read the src from within the iframe? If that is the case, not going to happen on a different domain because of the same origin policy. – epascarello Sep 26 '12 at 19:08

1 Answers1

2

See this question for how to convert a string to a location: Creating a new Location object in javascript

function convertStringToLocation(str) {
  var url = document.createElement('a');
  url.href = str;

  return url;
}

Then update your GetURLParameter function to accept any url:

function GetURLParameter(url, sParam) {
  var sPageURL = url.search.substring(1);
  // Everything past here is the same
}

Finally grab the src from your iframe:

var iframe = document.getElementById('myiframe');
var input = document.getElementById('externalsrc');
var url = convertStringToURL(iframe.src);
input.value = GetURLParameter(url, "externalsrc");
Community
  • 1
  • 1
  • How does the script finds the input which is within the iframe? second question is, should I put the convertStringToLocation() function inside the second one? Thanks, I'm almost there. – tleo Sep 26 '12 at 21:31
  • 1
    @Acanthuslabs If your input is inside the iframe, you can just use your original script inside the iframe. – zach_malone Sep 26 '12 at 23:32
  • Thanks, @zach_maline. Then, how would it be possible to read the current iframe's url within the iframe itself? How does this code: var sPageURL = window.location.search.substring(1); works within the iframe? can it be modified? – tleo Sep 28 '12 at 14:10
  • 1
    `window.location` should refer to the value in the `src` attribute in the ` – zach_malone Oct 02 '12 at 15:32