我有两个HTML页面,分别是
如何使用查询字符串将变量从
example1.html
和example2.html
。如何使用查询字符串将变量从
example1.html
传递到example2.html
,并在example2.html
中检索该变量,而不使用任何服务器端代码?example1.html
和example2.html
。example1.html
传递到example2.html
,并在example2.html
中检索该变量,而不使用任何服务器端代码?在example1.html中:
<a href='example2.html?myVar1=42'>a link</a>
<a href='example2.html?myVar1=43'>another link</a>
或者根据需要使用Javascript生成链接。只需确保?varName=value以某种方式添加到example2.html的末尾即可。
然后,在example2.html中,您可以使用Javascript解析example2传递的查询字符串。
为此,您可以尝试使用Querystring。
// Adapted from examples on the Querystring homepage.
var qs = new Querystring();
var v1 = qs.get("myVar1");
或者,parent.document.URL 包含了你所在页面的完整 URI。你可以提取它:
parent.document.URL.substring(parent.document.URL.indexOf('?'), parent.document.URL.length);
手动解析URI中编码的变量。
编辑:忘了'new Querystring()'中的'new'部分。糟糕...
<a href='example2.html?action=getAll&element=product&id=1'>a link</a>
<a href='example2.html?action=getAll&element=product&id=2'>another link</a>
点击此元素后构建“URL / ? action=getAll & element=product & id=1”的链接,在第二个页面中,您可以解析此链接并使用适当的参数调用相应的Ajax。