NoDoze Posted July 27, 2010 Share Posted July 27, 2010 I'm really rough with javascript...and need to do this from php: if (!empty($row['url'])) { echo 'url="' . $row['url'] . '" '; } ...in javascript... var html = "<span class='gmap-text-header'>" + name + "</span><br>" + if(url!=null) { document.write("<a href='" + url + "'>Link to Realtime Station</a>" +); } "</span>"; So that if there is no url, it only displays the name. And if there is a url it displays both the name and url. Thanks! Link to comment https://forums.phpfreaks.com/topic/209032-var-html-ifurlnullecho/ Share on other sites More sharing options...
KevinM1 Posted July 27, 2010 Share Posted July 27, 2010 You can't throw the result of a conditional in the middle of a string like that. Try: var html = "<span class='gmap-text-header'>" + name + "</span><br>"; if (url) { html += "<a href='" + url + "'>Link to Realtime Station</a>"; } html += "</span>"; It's just like building a string with PHP. Link to comment https://forums.phpfreaks.com/topic/209032-var-html-ifurlnullecho/#findComment-1091789 Share on other sites More sharing options...
NoDoze Posted July 27, 2010 Author Share Posted July 27, 2010 Ahhhhh!!! Never thought about it like that! Worked like a charm! Thanks! Link to comment https://forums.phpfreaks.com/topic/209032-var-html-ifurlnullecho/#findComment-1091800 Share on other sites More sharing options...
Alex Posted July 27, 2010 Share Posted July 27, 2010 Alternatively you could use the ternary operator. var html = "<span class='gmap-text-header'>" + name + "</span><br>" + (url ? "<a href='" + url + "'>Link to Realtime Station</a>" : "") + "</span>"; Link to comment https://forums.phpfreaks.com/topic/209032-var-html-ifurlnullecho/#findComment-1091803 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.