我目前正在使用以下方法从查询字符串中提取变量:
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
var s1 = getUrlVars()["s1"];
var s2 = getUrlVars()["s2"];
var s3 = getUrlVars()["s3"];
现在,我知道这已经做得很好。
我遇到的问题是:我需要在相应的参数后填充我的代码中的变量,但我不知道如何。
<body>
<div id="main">
<div id="header">
<div id="mainheader">
<a href="https://domain.com/index.php?s1=s1&s2=s2&s3=s3" target="_blank"><img id="topbanner" src="picture.gif"></a>
</div>
</body>
对此有任何帮助将不胜感激。
答案 0 :(得分:0)
我会使用Javascript在运行时动态创建您的URL。这样的事情应该有效:
<a href="#" onclick="RedirectURL();return false;"><img id="topbanner" src="picture.gif"></a>
然后在你的JS代码中:
function RedirectURL()
{
var s1 = getUrlVars()["s1"];
var s2 = getUrlVars()["s2"];
var s3 = getUrlVars()["s3"];
var URL = "https://domain.com/index.php?s1=" + s1 + "&s2=" + s2 + "&s3=" + s3;
window.location = URL;
}
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
答案 1 :(得分:0)
你有没有试过这个.. 为您的链接标记提供ID。
<a id="myLink" href="#" target="_blank"><img id="topbanner" src="picture.gif"></a>
然后你就可以做到这一点。
var s1 = getUrlVars()["s1"];
var s2 = getUrlVars()["s2"];
var s3 = getUrlVars()["s3"];
$("a#myLink").attr("href","https://domain.com/index.php?s1=" + s1 + "&s2=" + s2 + "&s3=" + s3);
答案 2 :(得分:0)
有几种方法可以做到:
体:
<body>
<div id="main">
<div id="header">
<div id="mainheader">
<a href="https://domain.com/index.php?" target="_blank"></a><br>
<a href="https://domain.com/index.php?" target="_blank"></a>
</div>
</body>
JS:
function getParams() {
var params = location.search.substring(1);
var arrParams = params.split('&');
var objParams = {};
for(var i=0; i<arrParams.length; i++) {
var arrParam = arrParams[i].split("=");
objParams[arrParam[0]] = arrParam[1];
}
return objParams;
}
var objParams = getParams();
var theURL = 'https://domain.com/index.php?s1=' + objParams["s1"] + "&s3=" + objParams["s3"];
$($('a')[0]).attr('href',theURL).attr('title',theURL).html(theURL);
身体:
<body>
<div id="main">
<div id="header">
<div id="mainheader">
<a href="https://domain.com/index.php?" target="_blank"></a><br>
<a href="https://domain.com/index.php?" target="_blank"></a>
</div>
</body>
JS:
function getParams2() {
var params = location.search.substring(1);
return params;
}
var Params = getParams2();
$($('a')[1]).attr('href','https://domain.com/index.php?' + Params).attr('title',$($('a')[1]).attr('href')).html($($('a')[1]).attr('href'));
全部放在一起:
<body>
<div id="main">
<div id="header">
<div id="mainheader">
<a href="https://domain.com/index.php?" target="_blank"></a><br>
<a href="https://domain.com/index.php?" target="_blank"></a>
</div>
</body>
<script type="text/javascript">
function getParams() {
var params = location.search.substring(1);
var arrParams = params.split('&');
var objParams = {};
for(var i=0; i<arrParams.length; i++) {
var arrParam = arrParams[i].split("=");
objParams[arrParam[0]] = arrParam[1];
}
return objParams;
}
var objParams = getParams();
var theURL = 'https://domain.com/index.php?s1=' + objParams["s1"] + "&s3=" + objParams["s3"];
$($('a')[0]).attr('href',theURL).attr('title',theURL).html(theURL);
function getParams2() {
var params = location.search.substring(1);
return params;
}
var Params = getParams2();
$($('a')[1]).attr('href','https://domain.com/index.php?' + Params).attr('title',$($('a')[1]).attr('href')).html($($('a')[1]).attr('href'));
</script>