/    Sign up×
Community /Pin to ProfileBookmark

Can JS ignore carriage returns?

The following breaks due to the carriage return in the str variable, returning ‘unterminated string constant’ error. Problem is I cannot control the carriage returns as they are entered by an outside system.

Anyone know if a way to force JS to ignore the carriage returns? I need to swap out the <td> contents, currently using .innerHTML.

Thanks for any help.

Problem code:

<script>
var str = “<p><b>test with inner</b></p>
<p”;
var x = 1;
function inner() {
if (x > 0) {
document.getElementById(“overviewText”).innerHTML = str;
}
}
</script>

<table border=”1″>
<tr><td><a href=”#” onClick=”inner();”>click…</a></td></tr>
<tr>
<td>
<div id=”overviewText”></div>
</td>
</tr>
</table>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzAug 11.2005 — Use concatenation.
var str = "&lt;p&gt;&lt;b&gt;test with inner&lt;/b&gt;&lt;/p&gt;[color=#3366FF]"+
"[/color]&lt;p";
Copy linkTweet thisAlerts:
@mikemaiauthorAug 11.2005 — This works in a known content setup, my setup is random. I'm building a template for handling content for users. They could type anything.

Good suggestion. Any other thoughts?
Copy linkTweet thisAlerts:
@Jonny_LangAug 11.2005 — [CODE]<HTML>
<Head>
<Script Language=JavaScript>

function inner(){

var str = document.getElementById('TD1').innerHTML;
str = str.replace(/<br>/gi,"").replace(/<p>/gi,"").replace(/</p>/gi,"").replace(/rn/g,"").replace(/"/g,"");
document.getElementById("overviewText").innerHTML = str;
//alert(str)
}

</Script>
</Head>
<Body>
<table border="1" width=200>
<tr><td><a href="#" onClick="inner()" id='TD1'>"<p><b>test with <br>inner</b>
</p>"</a></td></tr>
<tr>
<td>
<div id="overviewText"></div>
</td>
</tr>
</table>


</Body>
</HTML>[/CODE]
Copy linkTweet thisAlerts:
@KravvitzAug 11.2005 — So make whatever server-side language script you are using print out lines of concatenated JavaScript strings.
Copy linkTweet thisAlerts:
@mikemaiauthorAug 11.2005 — I like the .replace method:

1 - Drop the string into a <td> as the .html file is being generated by the content system.

2 - Run str.replace against it when rendered.

Thanks. I'll let you know how it works out. -Mike
×

Success!

Help @mikemai spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...