0
function Scale(){
    X = innerWidth / 1024;
    Y = innerHeight / 768;

    Z = document.getElementById("IFM");
    Z.style.transform = "scale(X, Y)";
}

I have a problem with this code. I couldn't use variables in scale!
What can I do with this problem?

1
  • How would you think are string contents distinguished from variables? Commented Feb 3, 2013 at 18:37

4 Answers 4

2

JavaScript has no inline string variables. All you can do is concatenate strings:

Z.style.transform = "scale("+X+", "+Y+")";

The numbers will be implicitly converted to strings.

You might as well use some custom sprintf-like format- or replace-methods, but usually concatenation with + is simpler.

Sign up to request clarification or add additional context in comments.

Comments

1

You either build the string with concats / plus or use String.prototype.replace.

Z.style.transform = "scale(" + X + "," + Y + ")";

or with a helper like

String.prototype.sFormat = function _simpleFormat( map ) {
    var myString    = this.toString(),
        args        = map instanceof Array ? map : Array.prototype.slice.call( arguments );

    while( ~myString.indexOf( '%r' ) ) {
        myString = myString.replace( '%r', args.shift() );
    }

    return myString;
};

.. and then go like

Z.style.transform = "scale(%r, %r)".sFormat(X,Y);

Comments

0

Change that line to:

Z.style.transform = "scale(" + X +", "+ Y + ")";

Comments

0

try this way "scale(" +X+ "," +Y+ ")";

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.