Solved

javascript pass variable to url

Posted on 2016-07-30
3
76 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

808 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