1

In the following standard function you can put in multiple arguments for the second and third parameter.

var retval = window.showModalDialog(dialog, varArgIn, varOptions);

The varOptions parameter can be filled with:

'dialogHeight:580px;dialogWidth:700px;center=yes;scroll=no', 

How do I get the only the value that's set at dialogHeight? So I can re-use only that value somewhere else.

4
  • I would do it with Regular Expressions... Commented Jan 11, 2013 at 13:38
  • Don't understand your problem. YOU fill the varOptions, don't you? Just create new variable for each option and re-use them all you want. Commented Jan 11, 2013 at 13:40
  • HenrikPeinar Just an example, it's an existing function I'll overwrite. algorhythm I tried, didn't get one to work. Commented Jan 11, 2013 at 13:41
  • Do you mean var dialogHeight = 580; var options = 'dialogHeight:' + dialogHeight +'px;dialogWidth:700px;center=yes;scroll=no';? Commented Jan 11, 2013 at 13:43

6 Answers 6

3

You can use simple regular expression:

'dialogHeight:580px;dialogWidth:700px;center=yes;scroll=no'.match(/dialogHeight:\w+/)[0]; //dialogHeight:580px

In your case, probably you'll need: varOptions.match(/dialogHeight:\w+/)[0];.

If you need only the value, you can use:

varOptions.match(/dialogHeight:(\w+)/)[1];.

As founddrama suggested, if dialogHeight is not a substring of varOptions the code will produce an error. Better way of doing the above thing is:

var match = varOptions.match(/dialogHeight:(\w+)/);
if (match && match.length >= 1) {
    console.log("The value is: " + match[1]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Though it's worth noting here that if varOptions does not contain dialogHeight then match will return null and trying optimistically to retrieve the indexed value of the match will throw an error.
1

Don't understand your problem. YOU fill the varOptions, don't you? Just create new variable for each option and re-use them all you want.

So something like

var dialogHeight = 580;
var dialogWidth = 700;
var center = 'yes';
var scroll = 'no';

var options = 'dialogHeight:'+ dialogHeight +'px;dialogWidth:'+ dialogWidth '+x;center='+ center +';scroll='+ scroll;
var retval = window.showModalDialog(dialog, varArgIn, options);
alert('Reusing dialog height vairable here:'+ dialogHeight);

If you want to get dialogHeight out from the string you gave, you could do the following:

var input = 'dialogHeight:580px;dialogWidth:700px;center=yes;scroll=no';
var dialogHeight = input.match(/(?!dialogHeight:)[0-9]*(?=p)/ ,input);
alert(dialogHeight);

http://jsfiddle.net/E67RF/ <- working fiddle with this code

2 Comments

No, I'm overwriting the normal functionality of the showModalDialog function don't ask why ;-) - function showModalDialogNEW(dialog, varArgIn, varOptions){ // jquery modal, with re-use of the dialogWith argument of the parameter
I improved my answer, see if that helps you.
1

If you are liable to need any of the other values it will be useful to have a function that converts the string of options into a dictionary-style object.

function getOptions(s) {
  var r = {}, kv;
  s = s.split(';');
  for (var i = 0; i < s.length; i++) {
    kv = s[i].split(s[i].indexOf(':') > -1 ? ':' : '=');
    r[kv[0]] = kv[1];
  }
  return r;
}
var opts = 'dialogHeight:580px;dialogWidth:700px;center=yes;scroll=no';
var optsObj = getOptions(opts);
console.log(optsObj.dialogHeight);

Comments

1

Use regular expressions in following way:

var s= 'dialogHeight:580px;dialogWidth:700px;center=yes;scroll=no';
alert(s.match(/dialogHeight:[\w]+\;/)[0].match(/:[\w]+;/)[0].replace(':',''));

5 Comments

Why you do all these strange things when you can extract the value with the regex?
Because goal for question is to extract value of attribute, not attribute and value together...
varOptions.match(/dialogHeight:(\d+px)/)[1]; extracts only the value without chaining all these functions together. The regex above simply uses regular expression groups. Here's a reference regular-expressions.info/brackets.html
ok you are right, but when value of attribute is do not contain only digits?
Yes it may contains auto too, so your use of \w is correct.
0

Assuming you don't already have the values, as in @Henrik Peinar's answer, you can parse the value from the string, (as long as it follows the conventions in your example).

Here are a couple ways you could do it:

1) Format the string to JSON notation, convert to object, access values

var objOptions = JSON.parse("{" + varOptions.replace(/;/g,",").replace(/=/g,":").replace(/(\w+)/g,'"$1"') + "}");

console.log(objOptions.dialogHeight);

2) Parse out only the value you're looking for using RegEx (my example is prone to errors, so I would do it a bit differently in production. This is just a quick example.)

varOptions.match(/dialogHeight:(\w+);/)[1]

3) Use split to get an array of the name-value pairs, then loop and split those results, testing the 'name' piece until you find the one you want. (I'm running out of time, so I've left off this example)

Comments

0
var parseDialogOptions = function() {
  var optionDivider = /\s*;\s*/
    , valueDivider = /\s*[:=]\s*/
    , whiteSpace = /^\s+|\s+$/g

  return function(optionString) {
    var options = optionString.split(optionDivider)
      , obj = {}
    for (var i = 0, l = options.length; i < l; i++) {
      var parts = options[i].split(valueDivider)
      if (parts.length == 2) {
        obj[parts[0].replace(whiteSpace, '')] = parts[1].replace(whiteSpace, '')
      }
    }
    return obj
  }
}()

Usage:

>>> parseDialogOptions('dialogHeight:580px;dialogWidth : 700px; center=yes ; scroll=no ; ')
Object { dialogHeight="580px", dialogWidth="700px", center="yes", scroll="no" }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.