1

I have some script written using the jQuery framework.

var site = {
link: $('#site-link').html()

}

This gets the html in the div site-link and assigns it to link. I later save link to the DB.

My issue is I don't want the html as I see this as being to dangerous, maybe?

I have tried:

 link: $('#site-link').val()

... but this just gives me a blank value.

How can I get the value inside the div without any markup?

6 Answers 6

2

Try doing this:

$('#site-link').text()

From the jQuery API Documentation:

Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.

Sign up to request clarification or add additional context in comments.

Comments

2

Use the .text() jquery method like this:

    var site = {
        link: $('#site-link').text()
    }

Here is an example of what .val(), .html() and .text() do: jsfiddle example

Comments

1

Use the text() method.

Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.

Comments

1

Use the .text() function of jQuery to get the only text.

var site = {
link: $('#site-link').text()

}

Comments

0

to avoid html, you will be required to use text() method of jquery.

var site = {
link: $('#site-link').text()

}

http://api.jquery.com/text/

Comments

0

If you are planning to store the result in the database and you are concerned about HTML, than using something like .text() rather than .html() is just an illusion of security.

NEVER EVER trust anything that comes from the client side!

Everything on the client side is replaceble, hijackable by the client rather easily. With the Tamper Data firefox plugin for example, even my mother could change the data sent to the server. She could send in anything in place of the link. Like malicious scripts, whole websites, etc...

It is important that before saving the "link" to the database you validate it on the server side. You can write a regex to check if a string is a valid url, or just replace everything that is html.

It's also a good idea to html encode it before outputting. This way even if html gets into your database, after encoding it will be just a harmless string (well there are other stuff to be aware of like UTF-7, but the web is a dangerous place).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.