Solved

javascript pass variable to url

Posted on 2016-07-30
3
88 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
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).
Suggested Courses

631 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