最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Checking if a variable is not null - Stack Overflow

matteradmin6PV0评论

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 ?

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 ?

Share Improve this question edited Nov 29, 2012 at 9:05 Nat Ritmeyer 5,6888 gold badges48 silver badges59 bronze badges asked Nov 29, 2012 at 9:02 John JohnJohn John 1
Add a ment  | 

3 Answers 3

Reset to default 2

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.

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>');

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>');
}
Post a comment

comment list (0)

  1. No comments so far