Search code examples
javascripthtmlcsscursor

How to put an url inside a string variable in javascript?


How can you include an url inside a string variable in javascript?

The resources I could find were all about retrieving urls from strings, which is not what I'm trying to do.

Some background information:

I'm trying to change the auto cursor with a javascript function to a custom defined cursor containing an url.

The default cursor of my webpage is a custom cursor defined in CSS, and looks like a circle. Thus far it works fine.

body{url(http://www.rw-designer.com/cursor-extern.php?id=14323) 20 20, auto;
}

Changing the cursor with a js function from "custom" to automatic works too:

document.body.style.cursor= "auto"

but changing the cursor back from automatic to custom doesn't work, I think because there is an url inside the string variable:

 document.body.style.cursor= "url(http://www.rw-designer.com/cursor-extern.php?id=14323) 20 20, auto;"

Putting backslashes before brackets,slashes and colons doesn't seem to work ,I've tried escape(url(...) and encodeURI like this but I don't think it works that way:

"url("+encodeURI(http://www.rw-designer.com/cursor-extern.php?id=14323))+"20 20 ,auto;"

any help would be appreciated

jquery answers welcome too.


Solution

  • You have an extra semicolon at JavaScript cursor field of style value:

    document.body.style.cursor= "url(http://www.rw-designer.com/cursor-extern.php?id=14323) 20 20, auto;"
    

    Try change it to:

    document.body.style.cursor= "url(http://www.rw-designer.com/cursor-extern.php?id=14323) 20 20, auto"
    

    When you modifying CSS styles via JavaScript - you should not put semicolon at the end of property value