John John
John John

Reputation: 1

Checking if a variable is not null

I am working on a CRM system that allows me to write custom HTML and JavaScript inside its interface, and I can reference CRM variables (such as current process status, date, etc) by using hash values #variable.variablename#. So I tried to write the following JavaScript but the problem is that no links will be displayed regardless of if any of the three variables exists or not:

<script type="text/javascript">
if ( #variable.1# != null) {
<div><a href="/Service1.svc/vf/img?imgid=5#variable.1#" >View Page</a></div> }

if ( #variable.2# != null) {
<div><a href="/Service1.svc/vf/img?imgid=5#variable.2#" >View Page</a></div>
}

if ( #variable.3# !=null) {
<div><a href="/Service1.svc/vf/img?imgid=5#variable.3#" >View Page</a></div>
}

</script>

What might be the problem ?

Upvotes: 1

Views: 7848

Answers (3)

Emil Ivanov
Emil Ivanov

Reputation: 37633

I would guest that the content of #variable.1# (2 and 3) isn't valid javascript. Maybe you need to enclose them in quotes, like "#variable.1#"

Also, you need to document.write the html - you can't just put it on a line and expect to appear.

<script type="text/javascript">
var html = '';
if ( "#variable.1#" != null) {
    html += '<div><a href="/Service1.svc/vf/img?imgid=5#variable.1#" >View Page</a></div>'
}

if ( "#variable.2#" != null) {
    html += '<div><a href="/Service1.svc/vf/img?imgid=5#variable.2#" >View Page</a></div>'
}

if ( "#variable.3#" !=null) {
    html += '<div><a href="/Service1.svc/vf/img?imgid=5#variable.3#" >View Page</a></div>'
}

document.write(html);

Also, consider using "View source" (Ctrl+U on Firefox) to see the actual Javascript, you've constructed.

Upvotes: 2

Try enclosing your CRM variables in single quotes, so they get interpreted as a string type in javascript, and use document.write to write html content, like this:

if ( '#variable.1#' != null) {
   document.write('<div><a href="/Service1.svc/vf/img?imgid=5#variable.1#" >View Page</a></div>');
}
if ( '#variable.2#' != null) {
   document.write('<div><a href="/Service1.svc/vf/img?imgid=5#variable.2#" >View Page</a></div>');
}

if ( '#variable.3#' !=null) {
   document.write('<div><a href="/Service1.svc/vf/img?imgid=5#variable.3#" >View Page</a></div>');
}

Upvotes: 0

user1726343
user1726343

Reputation:

Those are invalid JS statements. To write content to the document, use document.write:

document.write('<div><a href="/Service1.svc/vf/img?imgid=5#variable.1#">View Page</a></div>');

Upvotes: 1

Related Questions