Reputation: 31
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.
Upvotes: 1
Views: 130
Reputation: 422
You're close. You can read params like so:
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}
# example.com?test=1¶m1=heythere
$.urlParam('param1'); // heythere
$.urlParam('test'); // 1
Upvotes: 0
Reputation: 34556
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>
Upvotes: 1