Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.0 KiB

  1. <%
  2. ' iiaspstr.inc
  3. ' Function: sJSLiteral
  4. ' Arguments: String s [in]
  5. ' Return: String
  6. '
  7. ' Utility function to fix problems that can happen when using an ASP string
  8. ' variable as a JavaScript string literal. If the string variables may contain
  9. ' embeded single or double quotes (', ") or back slashes (\) then sJSLiteral
  10. ' should be used to escape these invalid characters
  11. '
  12. Const iSingleQuote = 39
  13. Const iDoubleQuote = 34
  14. Const sBackSlash = "\"
  15. Dim sLiterals
  16. sLiterals = Array( sBackSlash, "" & Chr(iSingleQuote), "" & Chr(iDoubleQuote) )
  17. Function sJSLiteral( s )
  18. Dim i, pos, length, sNew
  19. length = Len(s)
  20. pos = 0
  21. sNew = s
  22. For i = LBound(sLiterals) To UBound(sLiterals)
  23. Do
  24. pos = InStr(pos + 1, sNew, CStr(sLiterals(i)), 0)
  25. If pos > 0 Then
  26. sNew = Left(sNew, pos - 1) & sBackSlash & CStr(sLiterals(i)) & Right(sNew, length - pos)
  27. length = Len(sNew)
  28. pos = pos + 1
  29. End If
  30. Loop Until pos = 0
  31. Next
  32. sJSLiteral = sNew
  33. End Function
  34. %>