Solved

javascript pass variable to url

Posted on 2016-07-30
3
54 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
Comment Utility
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
Comment Utility
Thanks. Can you explain this?

 '"+x+"'

Open in new window

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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 …
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…

763 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

10 Experts available now in Live!

Get 1:1 Help Now