1

What can I do to attain the below thing... The argument generated is bit weird!

function function1(argument1,argument2)
{
if argument1 = "
") do something;
}

"argument1" and "argument2" are generated by the CMS. I can't do anything with those contents.

Either it will generate:

<script type='text/javascript'>
document.write(function1("argument1","argument2"));
</script>

OR

<script type='text/javascript'>
document.write(function1("
","argument2"));
</script>
  • The second code is already invalid JavaScript and cannot be parsed. There is nothing you can do about it in JavaScript. You have to fix it on server side. – Felix Kling Mar 19 '14 at 17:53
  • Hmm I can't do anything with that, its auto generated. Let me try some other way to attain the same. – Seven Mathew Mar 19 '14 at 18:18
  • The value might be autogenerated, but you it's you who builds the JavaScript code with it, right? I.e. you writing something like `function1("<% print argument 1 %>", ...)`. You have to escape the line breaks at this point. – Felix Kling Mar 19 '14 at 18:20
  • I don't know about that, can you make that above code bit more clear for me ? – Seven Mathew Mar 19 '14 at 18:35
  • How does the CMS inject the values in your JS code? – Felix Kling Mar 19 '14 at 18:36
  • CMS is blogger. It gives out the thumbnail URL. If no thumbnail is present, then it generates that thing. – Seven Mathew Mar 19 '14 at 18:40
  • So you are writing `function1()`? Or does `` generate the JS as well? Please post the code you are using to generate the above code. – Felix Kling Mar 19 '14 at 18:42
  • document.write(function1('','')); that's the code. – Seven Mathew Mar 19 '14 at 18:44
  • Ok. So you have to read in the blogger API how to escape values so that they are save to be used in JavaScript strings. Or maybe those tags are not even meant to be used in such a way. I don't have any experience with blogger. – Felix Kling Mar 19 '14 at 18:47

1 Answers1

1

You can use String.prototype.trim():

function function1(argument1,argument2)
{
    if(argument1.trim() == ''){
        // do something
    }
}

If you're worried about old browsers, you can implement the trim function yourself, see this question.

Community
  • 1
  • 1
MrCode
  • 63,975
  • 10
  • 90
  • 112