2016-02-16 127 views
1

我使用這個目前拉動變量從我的查詢字符串:如何填充變量的href

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

我會在運行時使用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; 
} 
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); 
0

有幾種方法可以做到這一點:

  1. 如果你要挑選你想從當前位置到新的URL傳遞其參數,可以使用這種方法:

身體:

<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>