0

I'm trying to extract multiple "parameters" from a string and for each parameter do something

e.g. If it detect myParam1=xxx, or myParam2=yyy etc.

In this code only one parameter can be detected, but I need multiple parameters:

function GetParameter(PPParam) {
    var input1 = document.getElementById('input1');
    var XXX = input1.value;

    var ZZZ = XXX.split('&');
    for (var i = 0; i < ZZZ.length; i++) {
        var sParameterName = ZZZ[i].split('=');
        if (sParameterName[0] == PPParam) {

            alert(sParameterName[1]);

            return sParameterName[1];
        }
    }
}

http://jsfiddle.net/Qmu5L/1/

Note: I need all the parameters will control a unique element,

for example to style a <div> (width=100, color=red, etc.)


escaped URL parameters statements if else switch

Community
  • 1
  • 1
  • i found this in another place, i just trying to understand how to modify it but it just a bit hard for me... can you show a little example pls? –  May 08 '14 at 17:01

2 Answers2

1

Here you go -- I've updated your Fiddle http://jsfiddle.net/Qmu5L/3/ based on Stratus3D's suggestion. It will produce a hash of all the parameters and their values.

I suggest you look at jQuery if you want to start applyinmg styles and messing with CSS -- makes it a lot easier.

function getParameters() {
  var searchString = document.getElementById('input1').value,
      params = searchString.split("&"),
      hash = {};

  if (searchString == "") return {};
  for (var i = 0; i < params.length; i++) {
    var val = params[i].split("=");
    hash[unescape(val[0])] = unescape(val[1]);
  }
  console.log(hash);
  return hash;
}

Once you've got the hash you can simply run it though a JQuery $.each function to style your DIV.

Mat
  • 1,668
  • 4
  • 23
  • 40
  • @user3602937 If this answer solves your original question please mark it as accepted. If you now have an additional question please search stackoverflow and see if your question has already been answered. If not please ask a separate question. – Stratus3D May 08 '14 at 17:49
  • ok how have i to start the new question: style a div using escaped URL parameters? –  May 08 '14 at 18:03
  • http://stackoverflow.com/questions/23549478/style-a-div-using-escaped-url-parameters –  May 08 '14 at 18:13
  • have a look here: http://stackoverflow.com/questions/23562330/escaped-url-parameters-statements-if-else-switch –  May 09 '14 at 12:03
0

A modified version of the function defined in this answer would work:

Get escaped URL parameter

You would just need to remove the unescape calls and replace the reference to window.location.search.substring with string parameter you want to process.

Community
  • 1
  • 1
Stratus3D
  • 4,648
  • 4
  • 35
  • 67
  • i am new in javascript, i tried to follow your advices but i cant make it working... –  May 08 '14 at 16:59
  • Or if you're using jQuery, try the excellent URL Parser Plugin: https://github.com/allmarkedup/jQuery-URL-Parser – Mat May 08 '14 at 17:00