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

javascript - How to fix this java script error handling single quote in the line - Stack Overflow

programmeradmin0浏览0评论

Hi I had a similar requirement as below and was curious to know to fix the issue.

Below is the code

<script type="text/javascript">
function Msg1(){
  var sd= document.getElementById('myText').innerHTML = 'Thank's!';
  alert('-sd-'+sd);
}
function Msg2(){
  document.getElementById('myText').innerHTML = 'Try messages 1 again...';
}
</script>
<input type="button" onclick="Msg1()" value="Show Message 1" />
<input type="button"  onclick="Msg2()" value="Show Message 2" />
<p id="myText"></p> 

The innerHTML tag is throwing script error like

Webpage error details


Message: Expected ';'
Line: 29
Char: 64
Code: 0
URI: file:///C:/Documents%20and%20Settings/j1007962/Desktop/spl.html

I was looking for the fix around innerHTML or better replace with something which would fix this . Thanks

Hi I had a similar requirement as below and was curious to know to fix the issue.

Below is the code

<script type="text/javascript">
function Msg1(){
  var sd= document.getElementById('myText').innerHTML = 'Thank's!';
  alert('-sd-'+sd);
}
function Msg2(){
  document.getElementById('myText').innerHTML = 'Try messages 1 again...';
}
</script>
<input type="button" onclick="Msg1()" value="Show Message 1" />
<input type="button"  onclick="Msg2()" value="Show Message 2" />
<p id="myText"></p> 

The innerHTML tag is throwing script error like

Webpage error details


Message: Expected ';'
Line: 29
Char: 64
Code: 0
URI: file:///C:/Documents%20and%20Settings/j1007962/Desktop/spl.html

I was looking for the fix around innerHTML or better replace with something which would fix this . Thanks

Share Improve this question asked Aug 1, 2011 at 6:43 GustyWindGustyWind 3,0363 gold badges42 silver badges51 bronze badges 2
  • I dont want to replace the message in double quotes as this is dynamically generated one – GustyWind Commented Aug 1, 2011 at 6:46
  • This may help for dynamically generated one stackoverflow./questions/97578/… – nidhin Commented Aug 1, 2011 at 6:57
Add a ment  | 

2 Answers 2

Reset to default 7

You should escape your quotes.

Modify

var sd= document.getElementById('myText').innerHTML = 'Thank's!';

To

var sd= document.getElementById('myText').innerHTML = 'Thank\'s!';

Or use double quotes

var sd= document.getElementById('myText').innerHTML = "Thank's!";

Try to escape the single quote

var sd= document.getElementById('myText').innerHTML = 'Thank\'s!';

Edit

Let say we have some text ing from Server side or from a HTML tag. In that case we have a valid variable. So there is no need to change that variable.

<div id="div1">
    Thank's!
</div>

<script language="javascript" type="text/javascript">

function changeDivHTML()
{
    var innerText = document.getElementById('div1').innerHTML;

    alert( innerText );
}

OR if the text is ing from server via Ajax

$.ajax({
    url: "test.html",
    success: function(data){
        alert( data.someStringValue );
    }
});
发布评论

评论列表(0)

  1. 暂无评论