|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Who can help escape apostrophe's N Javascript?
I'm using asp.net for my server side lang, and I assign a value to a label. when render the page it comes out like
<span id=something>Somethign from DB in here</span> My question is the span inner html could very well have 's. This is breaking my JavaScript. I tried to use the replace() but it only replaces the very first ' but not others after it. example: function cleanstring(str) { var mystr var i for (i = 0; i <= str.length; i++) { mystr = str.replace(/'/,"") } return mystr } I just want to replace 's with "" this is what calls the function function saveLinks(){ var cleanstr = cleanstring(document.getElementById('testing').inn erHTML) document.write ('<a href="javascript:addCookieArray(\'' + cleanstr + '\',\'' + location.href + '\');">save for later'); } this totally works with no apostrophes but breaks with more than 2 's. thanks |
|
#2
|
||||
|
||||
|
Replacing ' with " wont work either - CSS rules are not allowed to have any characters other than a-Z, 0-0 in them. Why not use the .net replace function?
|
|
#3
|
|||
|
|||
|
In JavaScript, to easily do multiple replaces of a character (apostrophes in this case), use the syntax:
mystr = mystr.split("'").join("\'"); Split creates an array of strings from your original seperated by the string parameter passed, and the Join takes that array and recreates the string, adding the string parameter passed between each segment. Another good use for this is with new line characters, where you can Split("\n") and Join("\\n"). |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Who can help escape apostrophe's N Javascript? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|