如何使用load jQuery传递参数

时间:2014-02-08 18:00:59

标签: jquery params

我正在尝试在父母中包含的子网络中传递params。

我在父母身上使用它:

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

在子网站(myPage.html)中显示参数值:

alert(getURLParameter('param1'));

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

但它不起作用。有没有想过阅读参数?

感谢。

2 个答案:

答案 0 :(得分:1)

由父脚本加载并包含在父脚本中的子脚本采用父级环境。

因此,在您的孩子的sscript中,location.search不是指向您用来加载孩子的路径,而是指向框架(或者,如果没有框架,窗口)URL。

您无法将params传递给正在加载的脚本,就像您当前尝试的那样。相反,在父脚本中声明它们并让您的子脚本从那里读取它们,例如

父脚本:

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

myPage.html下:

<script>alert(foo); //"bar"</script>

答案 1 :(得分:0)

你很亲密。你可以这样阅读params:

$.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
相关问题