简体   繁体   中英

Replace hard coded values in an array with values from another array with JavaScript?

I have a sharepoint site and want to create a script for links with querystring values that are not hard coded so I can use it on several sites without having to alter it. The HTML is a mess and I can't alter it.

I have some charts with links to a page with more details. On the page with charts I have some filters and when a user selects one of them the page reloads and the url then contains a querystring value.

With javascript I want the charts to get links depending on the querystring value so the page with more details reflects what the user selected in the filter.

The links from the charts looks something like http://mysite.com/details.aspx?filter=train

To get this link I have a script that creates them with this

goPage[0]= baseURL + filter[queryString];
goPage[1]= baseURL + filter[queryString];

I know the possible querystring values already (max 10) so I hard coded them in an array

filter[1] = 'road';
filter[2] = 'train';
filter[4] = 'horse';

So my question is, how do I do this if I don't know the querystring values on beforehand?

The links are dynamically added in the html document and I get all possible querystring values with:

  var businessUnitID = new Array();
  $('.table404').children().children().each(function(){
  var link = ($(this).find('a').attr('href'));
  var startIndex = link.indexOf(",'");
  var endIndex = link.indexOf("');");
  if ( startIndex >= 0 && endIndex >= 0 ) {
      var linkID = link.substring( startIndex+2, endIndex );
  businessUnitID.push(linkID);
  }
  alert(businessUnitID);
  });

Any ideas? Thanks in advance

edit: updated the question with more information

to get the queryString I use the following code:

//Get the filter ID from the querystring

function gup( filter )
{
  filter = filter.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+filter+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

var queryString = gup("SelectedID");

Also, instead of having road, train etc hard coded I get it with

  var businessUnitName = new Array();
  $('.table404').children().children().each(function(){
  var linkText = ($(this).find('a').text());
  businessUnitName.push(linkText);
  //alert(businessUnitName);
  });

So instead of having:

  var filter = new Array();
  filter[1] = businessUnitName[1];
  filter[2] = businessUnitName[2];
  filter[4] = businessUnitName[3];
  filter[5] = businessUnitName[4];
  filter[7] = businessUnitName[5];
  filter[9] = businessUnitName[6];
  filter[10] = businessUnitName[7];
  filter[11] = businessUnitName[8];
  filter[14] = businessUnitName[9];
  filter[15] = businessUnitName[10];

I want to, if for example the querystring value is the third value in the businessUnitID array, set filter to businessUnitName[3];

I hope I make myself clear here, please ask if you have any further questions.

I'm not sure what you're missing. Can't you just do:

for(var i=0; i < businesUnitID.length; i++) {
    goPage[i] = baseURL + businesUnitID[i];
}

However you could just add the baseURL when pushing the linkID into businessUnitID:

If you need to have businessUnitIDs correspond to specific numbers (0,1,2), then we need more information on how to match them up.

EDIT: based you your added info:

I would modify the way you collect the businesUnitIDs ...

var businessUnitID = new Array();
var i=0;
$('.table404').children().children().each(function(){
    var link = ($(this).find('a').attr('href'));
    var startIndex = link.indexOf(",'");
    var endIndex = link.indexOf("');");
    if ( startIndex >= 0 && endIndex >= 0 ) {
        var linkID = link.substring( startIndex+2, endIndex );
        businessUnitID[linkID] = i;
        i++;
    }
});

Then you can just use filter = businesUnitName[businesUnitID[queryString]]; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM