admin管理员组文章数量:1289539
new to this javascript thing. This is driving me crazy!
How do I add a URL in a string variable?
This is what I have:
function getRiskMessage(){
var msg = " visit our advice website at <a href=\'\'>this site</a>";
if(totalRiskScore > 25){
//this message (High Risk)
msg2 = show();
}//close if
return msg;
}
but when i output it to a div instead of a link 'this link' - I get the a tag and url as string like above.
What am I doing wrong, need to understand this.
new to this javascript thing. This is driving me crazy!
How do I add a URL in a string variable?
This is what I have:
function getRiskMessage(){
var msg = " visit our advice website at <a href=\'http://www.example.\'>this site</a>";
if(totalRiskScore > 25){
//this message (High Risk)
msg2 = show();
}//close if
return msg;
}
but when i output it to a div instead of a link 'this link' - I get the a tag and url as string like above.
What am I doing wrong, need to understand this.
Share Improve this question edited Jul 14, 2015 at 21:27 Maurice Greenland asked Jul 14, 2015 at 21:00 Maurice GreenlandMaurice Greenland 3152 gold badges5 silver badges20 bronze badges 2- 2 "when i output it to a div" - show how you do it. – dfsq Commented Jul 14, 2015 at 21:04
-
Where is the
show()
function. You're assigning themsg
variable from the return value of theshow()
function and then returning that. – jfriend00 Commented Jul 14, 2015 at 21:11
3 Answers
Reset to default 4tl;dr You're using createTextNode
, which creates a plain-text node. Don't do that :) Use the innerHTML
property instead. See below for more details.
You don't need to escape quotes unless they resemble the start and end quotes:
var foo = "Visit <a href='http://bing.'>Bing</a>.";
In the above, the string begins and ends with double-quotes, so single-quotes can be used without any problems.
var foo = "Visit <a href=\"http://bing.\">Bing</a>.";
In the above, we use double-quotes around the string, and around the [href] attribute. As a result, the inner-quotes (around the attribute) must be escaped so they don't confuse the parser.
Lastly, when you output, make sure to output as HTML:
div.textContent = foo; // Represents foo as plain text
div.innerHTML = foo; // Interprets foo as HTML
div.appendChild( document.createTextNode( foo ) ); // Similar to textContent
div.appendChild( document.createElement( "span" ) ).innerHTML = foo; // HTML
Example:
document.querySelector( ".msg" ).innerHTML = "Visit <a href='http://bing.'>Bing</a>.";
<div class="msg"></div>
Thy this:
var msg = document.createElement('span');
msg.innerHTML = " visit our advice website at <a href=\'http://www.example.\'>this site</a>"
container.appendChild(msg);
Working demo: FIDDLE
This is a basic example on how to insert html using plain javascript:
document.getElementById('your-div').innerHTML = msg
Here is a fiddle of your case: demo
本文标签: Javascript How to include a url in a string variableStack Overflow
版权声明:本文标题:Javascript: How to include a url in a string variable? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741466094a2380321.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论