Jump to content

JavaScript Problem

robeng
Go to solution Solved by SCHISCHKA,
19 minutes ago, robeng said:

document.getElementById("bildfram2").style.backgroundImage = "url(" + bilder[count] + ")";

 

try this ^

Hi! I have a script that performce this action:

document.getElementById("bildfram2").style.backgroundImage = "url("bilder[count]")";

I have an array looking like this:

var bilder = ["../img/startsida/nature1.jpg","../img/startsida/nature2.jpg","../img/startsida/nature3.jpg","../img/startsida/nature4.jpg"];

I get this error from the first line of code: 

Uncaught SyntaxError: Unexpected identifier

Can someone help me fix the issue! Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

One problem I can see immediately is that you've got double quotes nested inside of double quotes, which divides up the string into "url(" and ")". The nested set of quotation marks will need to be 'single quotes.'

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, typographie said:

One problem I can see immediately is that you've got double-quote nested inside of double quotes, which divides up the string into "url(" and ")". The nested set of quotation marks will need to be 'single quotes.'

but then it will set the background-image to bilder[count]

Link to comment
Share on other sites

Link to post
Share on other sites

19 minutes ago, robeng said:

document.getElementById("bildfram2").style.backgroundImage = "url(" + bilder[count] + ")";

 

try this ^

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, SCHISCHKA said:

try this ^

Thanks that worked! But... now it doesent fine the images. So the script is located in a script foler... and img in a img folder. Is't it ../img/image

To back and then go into img folder?

 

EDIT: Well... aparently I don't need the ../ here in this script...

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, robeng said:

Thanks that worked! But... now it doesent fine the images. So the script is located in a script foler... and img in a img folder. Is't it ../img/image

To back and then go into img folder?

 

EDIT: Well... aparently I don't need the ../ here in this script...

The URL would be based on the page's URL not the script's URL. I have a script on every page which defines useful variables. in that script ill have a function that returns the root folder. that way i just have one place to update code if i want to rename a folder or move files around. Kind of like how you would handle API keys. If you get into jquery you will see this sort of thing in use

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

may I ask how you're getting count? If it is from a url then you're opening yourself up to xss attacks.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, M.Yurizaki said:

Could just be


var count = bilder.length;

Of course that's assuming OP wrote variables that are self describing.

that would only give you the last image. :/ either way something to think about.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×