Testing for Variable Existing

346 views
Skip to first unread message

Francois Laberge

unread,
Aug 3, 2011, 2:21:15 PM8/3/11
to Embedded JavaScript
Is it possible to test if a variable has been defined? For example if
I don't pass a 'title' variable into the template, can I detect that
and supply a default or different behavior?

The following code fails when no loginForwardUrl is passed in from my
Node.js app.

<% if(loginForwardUrl!=undefined) {%>
loginForwardUrl = "<%= loginForwardUrl %>";
<% } %>

I get this error
ReferenceError: /Users/francoislaberge/Sites/playbrassmonkey.com/views/
index.ejs:3
1| <html>
2| <head>
>> 3| <% if(title!=undefined) {%>
4| <title><%= title %> - Brass Monkey</title>
5| <% } else {%>
6| <title>Brass Monkey</title>

I banged my head agains this for too long. It seems so basic.

Thanks in advance,
Francois

Justin Meyer

unread,
Aug 3, 2011, 2:33:59 PM8/3/11
to Embedded JavaScript
try if(this.loginForwardUrl)

Max Hwang

unread,
Aug 3, 2011, 2:39:38 PM8/3/11
to embeddedj...@googlegroups.com
Use the JS function typeof:

It'd be something like

If (typeof(loginForwrdUrl) != 'undefined') {
// blah blah
}

-Max

Thanks in advance,
Francois

--
You received this message because you are subscribed to the Google Groups
"Embedded JavaScript" group.
To post to this group, send email to embeddedj...@googlegroups.com.
To unsubscribe from this group, send email to
embeddedjavascr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/embeddedjavascript?hl=en.


Francois Laberge

unread,
Aug 3, 2011, 2:42:00 PM8/3/11
to embeddedj...@googlegroups.com
Thanks! That worked.

try if(this.loginForwardUrl)
--
You received this message because you are subscribed to the Google Groups "Embedded JavaScript" group.
To post to this group, send email to embeddedj...@googlegroups.com.
To unsubscribe from this group, send email to embeddedjavascr...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/embeddedjavascript?hl=en.




--
Francois Laberge
Chief Innovation Officer
Brass Monkey Inc.
2735 Mission Street - Apt. 25
San Francisco, CA, 94110

Reply all
Reply to author
Forward
0 new messages