1

I'm trying to pass params in a child web included in a parent.

I'm using this in the parent:

$('#idElemet').load('myPage.html?param1='+value);

In the child web (myPage.html) to show the param value this:

alert(getURLParameter('param1'));

function getURLParameter(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

But it doesn't work. Any idea to read the params?

Thanks.

0

2 Answers 2

1

A child script loaded by and subsumed into a parent script adopts the parent's environment.

So in your child sscript, location.search points not to the path you used to load the child, but to the frame (or, if no frames, window) URL.

You cannot pass params to a script being loaded as you are currently trying to do. Instead, declare them in your parent script and have your child script read them from there, e.g.

Parent script:

var foo = 'bar';
$('#idElemet').load('myPage.html');

myPage.html:

<script>alert(foo); //"bar"</script>
Sign up to request clarification or add additional context in comments.

1 Comment

A loop is generating div and call the load function on every div so the variable value is lost in each iteration.
0

You're close. You can read params like so:

$.urlParam = function(name){
    var results = new RegExp('[\\?&amp;]' + name + '=([^&amp;#]*)').exec(window.location.href);
    return results[1] || 0;
}

# example.com?test=1&param1=heythere
$.urlParam('param1'); // heythere
$.urlParam('test');   // 1

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.