CSS and JQuery: spaces inside image name break code of url()
Spaces are not valid in a URI. They need to be encoded to %20
.
You could src.replace(/ /g, '%20')
, or more generally, encodeURI(src)
to %
-encode all characters that aren't valid in a URI. encodeURIComponent(src)
is more common, but it would only work if the src
was just a single relative filename; otherwise, it'd encode /
and stop paths working.
However, the real problem is that the original img src
is already broken and only working thanks to browser fixups correcting your error. You need to fix the Ruby script generating the page. You should be URL-encoding the filename before including it in the path; there are many more characters that can cause you problems than just space.
As Pekka said, you should also use quotes around the URL in the url(...)
value. Whilst you can get away without them for many URLs, some characters would have to be \
-escaped. Using double-quotes mean you can avoid that (no double-quotes can appear in a URL itself).
Change img src Attribute with spaces in name
As promissed (if a bit delayed), the solution was to wrap the string in "unescape", which forces thing like spaces to NOT be interpreted when used.
var newSrc = unescape("images/" + sname[field].split(":")[selected] + ".png");
Jquery forward slashes are being replaced by spaces within tags
Your problem is this
style='background-image: url('"
You have a single quote around the background image css, and then a single quote around the url. That is going to cause it to terminate the style tag instantly after "url(". Everything after that is registered as a new attribute, and that just results in the bizarre html where the image name seems to be interpreted as an attribute.
Try "\" instead of the single quotes around the url. If that doesn't work, try rearranging what uses single quotes, double quotes, and escaped quotes until it generates the correct html. I've had this exact problem. That has always resolved it.
After edit:
$('#someId').append("<div class='request-quote__dropdown'>
<div class='request-quote__dropdown-image'
style='background-image: url(\""+element.parent.PictureUrl+"\");'>
</div></div>")
How should I escape image URLs for CSS?
Is escaping the quotes enough?
No, you also should worry about backslashes and newlines.
Here is the CSS grammar for a double quoted URI:
http://www.w3.org/TR/CSS21/grammar.html#scanner
"([^\n\r\f\\"]|\\{nl}|{escape})"
where {nl}
is
\n|\r\n|\r|\f
and {escape}
is a backslash-escaped character. So a trailing backslash will break your CSS. A non-escaped newline likewise.
I would strongly recommend to remove all whitespace and finally escape "
and \
Is jQuery rewriting my CSS attributes?
You should ALWAYS use +
or %20
in place of a space in URLs:
<div style="background-image:url(/images/image+one.png);">
Is quoting the value of url() really necessary?
The W3C says quotes are optional, all three of your ways are legal.
Opening and closing quote just need to be the same character.
If you have special characters in your URL, you should use quotes or escape the characters (see below).
Syntax and basic data types
The format of a URI value is 'url(' followed by optional white space followed by an optional single quote (') or double quote (") character followed by the URI itself, followed by an optional single quote (') or double quote (") character followed by optional white space followed by ')'. The two quote characters must be the same.
Escaping special characters:
Some characters appearing in an unquoted URI, such as parentheses, white space characters, single quotes (') and double quotes ("), must be escaped with a backslash so that the resulting URI value is a URI token: '\(', '\)'.
Extra space in my figure
Images are displayed inline by default, and space is left for character descenders per line-height.
http://jsfiddle.net/2drYk/22/
.content {
display: block;
}
Related Topics
Python Library for Rendering HTML and JavaScript
How to Save Canvas Animation as Gif or Webm
How to Limit Options Selected in a HTML Select Box
How to Get a Floating Footer to Stick to the Bottom of the Viewport in Ie 6
Jquery-Ui Datepicker Change Z-Index
Why Is Arr = [] Faster Than Arr = New Array
Js & Jquery Can't Detect HTML Elements, and Say's They Are Undefined
Best Way to Get All Selected Checkboxes Values in Jquery
HTML Button to Not Submit Form
$ Variable (Dollar Sign) in Chrome
Angular2 Router 2.0.0 Not Reloading Components When Same Url Loaded with Different Parameters
How to Separate Web Components to Individual Files and Load Them
"Scroll" to the Very Right of a Long Text Input
How to Stop User from Printing Webpages? Using JavaScript or Jquery
Generating CSS Media Queries with JavaScript or Jquery
Mongoose - What Does the Exec Function Do
How to View Events Fired on an Element in Chrome Devtools
Sending Message from a Background Script to a Content Script, Then to a Injected Script