Solved

javascript pass variable to url

Posted on 2016-07-30
3
62 Views
Last Modified: 2016-08-04
I am trying to pass a simple variable to url but for some reason it is not working. Am I missing something?

function myFunction() {
    var x = "img_tree.png";
    document.body.style.backgroundColor = "#f3f3f3";
    document.body.style.backgroundImage = 'url(x)';
}

Open in new window

0
Comment
Question by:Mazdajai
  • 2
3 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 41736208
Check this:
function myFunction() {
    var x = "img_tree.png";
    document.body.style.backgroundColor = "#f3f3f3";
    document.body.style.backgroundImage = "url('"+x+"')";
}

Open in new window

And that script does not PASS anything to URL.
The script does set the background image to a image file denoted by the url parameter:
http://www.w3schools.com/jsref/prop_style_backgroundimage.asp
0
 
LVL 21

Author Comment

by:Mazdajai
ID: 41736241
Thanks. Can you explain this?

 '"+x+"'

Open in new window

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 41737268
No, that I cannot explain.
But I can explain you this:
"url('"+x+"')";

Open in new window

In JavaScript that are three Strings concatinated to one string value.
In the resulting string value is the middle part enclosed by single quotes and the middle part is comming from variable with the name: x

Is that explanation suitable for you?
1

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now