Skip to content Skip to sidebar Skip to footer

Html/javascript Link To A Local File

I'm trying to show a link to a local file using javascript, and it isn't working. I'm not sure what I'm doing wrong. the html is: &

Solution 1:

There are few corrections in your page:

<scripttype="text/javascript"src='http://jquery.com'></script><!-- This is Absolutely Wrong --><scripttype="text/javascript"src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js'></script>

And To Load Local scripts use: file:/// for it:

So:

<scripttype="text/javascript"src="C://wamp/www/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js"></script><!-- Also It is C:/ not C:// -->

will be:

<scripttype="text/javascript"src="file:///C:/wamp/www/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js"></script>

By Using file:/// and C:/ instead of C:// for all <script>'s src and <a>'s, <link>'s href will solve your problem.

But As you are using wamp, Switch it on and use http://localhost, also using relative paths to the page will be much more easy. In general, it is considered best-practice to use relative URLs, so that your website will not be bound to the base URL of where it is currently deployed. For example, it will be able to work on localhost, as well as on your public domain, without modifications.

For more see: Absolute vs relative URLs and http://www.webdeveloper.com/forum/showthread.php?208825-lt-script-gt-with-source-as-a-local-file

Hope it'll solve the problem.

Solution 2:

If you're running the script locally (via your browser's File -> Open menu option) then you don't need the drive letter, unless it's on a different drive. If you want to use whatever path you want, use file:// in front. In other words, you can use file:// anywhere you could use http://.

Remember, all paths are relative to the script location.

Solution 3:

When you do that the link will not download the file to your client, I suppose this is what you want.

If this page is going to be served over the internet, you should not be using URLs relative to your computer, instead relative to the server computer:

  1. I would search what is the folder that the web server is... serving :p. Which in your case seems to be: c:\wamp\www
  2. Try changing the link href to /Projects/tile/name.docx. This is the URL relative to the server, which means that when your user is in your page, for example: http://localhost, and clicks the link, user would try to go to: http://localhost/Projects/tile/name.docx.
  3. The server would search in its file system c:\wamp\www\Projects\tile\name.docx.

Note how the file system URLs c:\wamp\www\.. and URLs of the form http://localhost/... are different.

In this case you want in your page an URL of the form http://... , because if you use a file system URL when the person looking at your page clicks the link the browser would search the file in the person computer, and not in the server computer.

Also, you could want to remove c:/wamp/www/ in every place you have it and just leave /.

I hope I did not miss the point.

Post a Comment for "Html/javascript Link To A Local File"