0

First off, I know that this sort of a duplicate, but I believe that my problem is different from the other question on the same topic (read: more n00by).

Basically, I'm trying to use this script. To make my code neater and more easily human-readable, I decided to put it into a seperate script, wrap the whole thing in a function with a path parameter, and then call the function (after setting up the script on the main page) with a the path to the image I wanted to use.

My question is pretty basic; what's the correct way to format the path? Do I format the path when I pass it to the function as a string? Right now, what I have isn't working.

1 Answer 1

1

The path will be always a string but the path format depends.

You can use either relative or absolute path.

I'd recommend you to try the relative path first. For example, if your image is in a parent folder from your page, use var cursorpath="../cursor.gif";.

For absolute paths you need to provide the full path and match the same protocol used for your page. For example, if your image is in C:/dev/images/cursor.gif and you're running your page directly from the file, you should use var cursorpath="file:///C:/dev/images/cursor.gif".

Let me know if this solve your problem.

EDIT

If the image is in a folder at the same level of your page use:

cursorpath="img/cursor.gif"; //Without the '/' in the beginning 

For example, if we are at the page http://google.com/search/index.html and add two <img> like this:

<img src="images/sample.png">
<img src="/images/sample.png">

The first one will search for an image at http://google.com/search/images/sample.png.

The second one will try this path beggining from the website root (the /), like this http://google.com/images/sample.png.

Sign up to request clarification or add additional context in comments.

7 Comments

So I'm trying to use an image in a folder called img. The folder is in the same directory as the index.html file that I'm calling the function from. In this case, would I use var cursorpath="/img/cursor.gif";?
And, if I'm passing this directory to a function instead of as a variable, would I keep the quotes?
@evamvid Yes keep the quotes, they can be either double or simple.
So I would apply this the same way when passing an argument to the function? The original script uses cursorpath as a variable. My objective is to wrap it in a function called cursor, with path as an argument. cursorpath would be set equal to path.
If you declared the function like this: function cursor(cursorpath){ ... } you already have the cursorpath variable inside that function. So you can call like this: cursor("img/cursor.gif");. Got it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.