最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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>');
}
发布评论

评论列表(0)

  1. 暂无评论