Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3332436
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:41:17+00:00 2026-05-17T23:41:17+00:00

UPDATE : Once I looked at the problem in Firebug, I found my mistake

  • 0

UPDATE: Once I looked at the problem in Firebug, I found my mistake immediately. And it was an embarrassing unmatched double quote that I must have deleted somehow. I had been using Chrome’s developer window. Very sorry for using up your resources. But, lesson learned! (“I hope.)

What is the best way for me to escape html characters that I want to send to my server? I am using jQuery, .ajax(), and jsonp.

I’m writing a bookmarklet which sends parts of the current page’s html to my server. Here is the ajax call:

jQuery.ajax({
    url: 'http://www.my_server.com/file.php?callback=?',
    dataType: 'jsonp',
    data: { someHtml: escape(jQuery(this).html().substring(0,1000)) },
    success: function() { // stuff },
    beforeSend: function(xhr) {
                  xhr.setRequestHeader('Content-type','text/html');
                },
    error: function() { // stuff }
});

I need to use JSONP and therefore I can’t use POST, and this is why I’m truncating the html data. Things work if the html is “nice”, but if it contains characters javascript doesn’t like, then I have problems. I fixed my ‘ problem by using escape(), but now I think I’m having newline and tab problems.

Chrome’s dev console gives me the same error:

Uncaught SyntaxError: Unexpected token <

which I assume means some character is causing things to break out of javascript. I have tried the following: escape(), encodeURI/Component(), serialize(), text(), but nothing has worked yet. At first, I didn’t use beforeSend, but thought I should try it, but no difference.

Currently, I’m stuck with some html which has a line break, then a tab, then a couple of spaces. I have tried replacing these characters using replace():

... .substring(0,1000).replace(/(\r\n|[\r\n])/g,'')

I found this regex string on another site which is supposed to replace various combinations of carriage returns and line feeds.

I hope I’ve explained myself clearly enough. It’s my first question at Stack Overflow so go easy on me. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:41:17+00:00Added an answer on May 17, 2026 at 11:41 pm

    You don’t need to escape or encode. jQuery will take care of properly URL encoding the data:

    data: { someHtml: $(this).html().substring(0, 1000) },
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

During a complicated update I might prefer to display all the changes at once.
Our application uses a large product database that is updated once a day. Updates
Update: Now that it's 2016 I'd use PowerShell for this unless there's a really
UPDATE - A comprehensive comparison, updated as of February 2015, can be found here:
I have an ASP.NET application that uses Session.SessionID to prevent multiple users viewing the
Here's some code I saw once. Can you see what's wrong with it? [updated]
Update: Solved, with code I got it working, see my answer below for the
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update: giving a much more thorough example. The first two solutions offered were right
UPDATE: Focus your answers on hardware solutions please. What hardware/tools/add-in are you using to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.