How to get the file using it's URL in Javascript?

Properties of a URL

  • I wrote a slide show javascript and also downloaded similar scripts from the web. Created HTML files using AceHTML PRo software for all these slide show files. These files are on my C drive. I created a file for all my pictures on my D drive with extension jpg. On testing each slide show HTML code file in IE, Maxthon and Opera, the first picture shows and not the rest of the remaining pictures. Whenever I click next, for more pictures, I get a blank page with a little red x where the picture was to have been. Right clicking the red x and going to Properties revealled this statement at the URL "file:///d:%02.jpg" This is my code: "var picture = new Array("d:\1.jpg", "d:\2.jpg", "d:\3.jpg") Can you explain File:///d:%02.jpg?

  • Answer:

    Dear omoolorun, The reason your page correctly displays the first picture is that its location is specified by the HTML code as src="d:\1.jpg", which translates directly into the string "d:\1.jpg". But when you click on the "Next Picture" link and the JavaScript gets called, the next element of the array is "d:\2.jpg", which gets translated by the JavaScript interpreter into the invalid location "d:%02.jpg". This is because the backslash character, "\", has a special meaning in JavaScript that causes it to render special characters. If you intend a JavaScript string to include the backslash character itself, you must write it twice in a row: "\\". Thus, when you are using a JavaScript string to specify the file location d:\2.jpg, you should write it as "d:\\2.jpg". You will find that the HTML source code below achieves the intended effect. If you wish to preserve your own prefatory tags, you can just cut the script out of your current file and paste in my own as a substitute. If you have any trouble carrying out this substitution, please advise me through a Clarification Request so that I can make sure everything is working at your end before you rate my answer. Regards, leapinglizard <html> <head> <script language="JavaScript"> //<!-- var picture = new Array("d:\\1.jpg", "d:\\2.jpg", "d:\\3.jpg", "d:\\4.jpg", "d:\\5.jpg", "d:\\6.jpg", "d:\\7.jpg", "d:\\8.jpg"); var picNumber = 1; var numberofPics = picture.length; function nextPicture() { if (picNumber < numberofPics){ picNumber++; } else { picNumber = 1; } //alert(picture[picNumber-1]); document.myPicture.src = picture[picNumber-1]; } //--> </script> </head> <body> <center> <img src="d:\1.jpg" name="myPicture" width=400, height=400> <p> <a href="JavaScript:nextPicture()">Next Picture</a> </p> </center> </body> </html>

omoolorun-ga at Google Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.