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 4605348
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:25:12+00:00 2026-05-22T00:25:12+00:00

My javascript reads data from a query string and puts that data into a

  • 0

My javascript reads data from a query string and puts that data into a text box using jQuery.val().

This works fine but I am wondering is this safe from XSS attacks?

Say the query string looked like…

site.com?q="javascript:alert(document.cookie)

Which would effectively do:

jQuery.val('"javascript:alert(document.cookie)')

From what I have tested in IE8 / firefox this sets the input value as seen and doesn’t do the actual injection.

If I run this function over the string first:

function htmlEncode(str) {
    return str.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#039;').replace(/"/g, '&quot;');
}

Then you literally see &quot;javascript:alert(document.cookie) in the input value which is not what I want.

Using jQuery 1.5.2 I guess my question is does jQuery.val() handle the HTML entities for you and is therefore considered safe?

  • 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-22T00:25:13+00:00Added an answer on May 22, 2026 at 12:25 am

    Given the following:

    jQuery("#SomeTextbox").val("new value for SomeTextbox")
    

    the jQuery code for the val function simply does this:

    this.value = "new value for SomeTextbox";
    

    where this is a reference to the Text object in the DOM that represents the textbox with id “SomeTextbox”. The string "new value for SomeTextbox" is stored as the value property of that DOM object. It does not get transformed or sanitized in any way. But it doesn’t get parsed/interpreted by the JavaScript engine either (e.g. as it would with InnerHTML). So regardless of what your argument to val is, it isn’t going to “do” anything. It just changes the value of a string property of an object in the DOM. So, yes, it would be safe.

    EDIT:

    Here is some additional information that you may find helpful.

    In general, putting something into a text box, no matter how malicious it may appear, and regardless of how it gets there is “safe” as long as it stays in the text box. But it matters a lot where it goes from there.

    If the content of the textbox is subsequently rendered in a stream of parsed HTML, then it is no longer safe. A common scenario is to store the content of a textbox in a database, then retrieve it later and render it in a context where the browser parses is as HTML. If the re-display occurs in the context of a different user, it creates an opportunity for a malicious user to enter data into the textbox in order to gain access to another users private information at some future time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got index of my elements: <h2>Index</h2> <script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js></script> <script type=text/javascript> $(document).ready(function ()
I am trying to create a drop down list using jQuery, PHP and mySQL,
I have a very simple class using HttpListener to receive an HTTP POST from
I'm currently creating a very small form on my homepage using HTML and JavaScript.
How can I read the username and password from http GET in .NET MVC
I have been struggeling all day to read a json file from server and
I am using the Uploadify Plugin to upload the picture, and i am doing
$(document).ready(function() alert($(td).val()); }); But the alert is empty. How do I select the values
I created a function to take location input, pass it to Google Maps Javascript
I'm trying to acquire a JSON which is being sent from an https secure

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.