1

So I'm trying to do two things, first is outputting url variables into a javascript string and then use that string within an iframe.

The URL is: https://testurl.com/report.html?name=Jack&zip=rg1&city=Reading

The code I have so far is:

<script>
function GetUrlValue(VarSearch) {
    var SearchString = window.location.search.substring(1);
    var VariableArray = SearchString.split('&');
    for (var i = 0; i < VariableArray.length; i++) {
        var KeyValuePair = VariableArray[i].split('=');
        if (KeyValuePair[0] == VarSearch) {
            return KeyValuePair[1];

            var name = GetUrlValue('name');
            var zip = GetUrlValue('zip');
            var city = GetUrlValue('city');
        }
    }
}

var iframetesting = 'https://testurl.com/report.html?name='+name+'&zip='+zip+'&city='+city+'&country=GB';
</script>

When I put it into an alert it says undefined.

Once thats done, I then need to put that into an iframe source.

<iframe src="javascript:myfunction()" width="100%" height="2300px" frameborder="0">


</iframe>

If anyone could help me solve where I am going wrong that would be great.

Thanks

4 Answers 4

1

It's because your var declarations are scoped inside GetUrlValue, and then not accessible from outside.

function GetUrlValue(VarSearch) {
    var SearchString = window.location.search.substring(1);
    var VariableArray = SearchString.split('&');
    for (var i = 0; i < VariableArray.length; i++) {
        var KeyValuePair = VariableArray[i].split('=');
        if (KeyValuePair[0] == VarSearch) {
            return KeyValuePair[1];
        }
    }
}

// e.g your url is http://www.x.com?name=x&zip=78&city=NY

var name = GetUrlValue('name'); // output x
var zip = GetUrlValue('zip'); // output 78
var city = GetUrlValue('city'); //output NY
0

Try this:

<iframe id="my-iframe" width="100%" height="2300px" frameborder="0"></iframe>

<script>
    document.getElementById('my-iframe').src = myfunction();
</script>
0

The variables "name", "zip" and "city" are defined within the innermost function, and are only visible there.

A quick solution to your problem would be to define the variables outside any function at global scope (which is always a bad idea) just to get you started.

Try

var zip;
var city;
var name;

at the top of your script.

0

iframestring will not understant name,zip & city because they are local variable to function.

A varibale scope is a context in which variable exist. The scope specifies from where you can access a variable and wether you have access to the variable in that context. JS does not have block level scope, instead they has function level scope. Variable declare within a function are local variable and are only accessible within that function or by function inside that function(closure).

<script>
var name= null;
var zip= null;
var city= null;
function GetUrlValue(VarSearch){
        var SearchString = window.location.search.substring(1);
        var VariableArray = SearchString.split('&');
            for(var i = 0; i < VariableArray.length; i++){
            var KeyValuePair = VariableArray[i].split('=');
                if(KeyValuePair[0] == VarSearch){
                    return KeyValuePair[1];

                        name= GetUrlValue('name');
                        zip= GetUrlValue('zip');
                        city= GetUrlValue('city');
                    }
                }
             }


    var iframestring = 'https://testurl.com/report.html?name='+name+'&zip='+zip+'&city='+city+'&country=GB';

</script>

Hope this will help you

2
  • I tried this and set it to alert to test, but its outputting name=null etc. Does the iframestring need to be within the }}
    – Sam Allen
    Sep 24, 2015 at 11:24
  • You may need to make couple of changes in your function. You are calling GetUrlValue() from the GetUrlValue() function itself.This is wrong if you check the debugger tool you may see a memory leak or out of memory issue.I have restructured the code and you can validate from here jsfiddle.net/rse7na4v/2 .Secondly I tested this line with your url you shared. This is returning an empty string
    – brk
    Sep 24, 2015 at 13:34

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.