Solved

Javascript issue (wrong path)

Posted on 2014-01-08
12
257 Views
Last Modified: 2014-01-15
I have a button that activates a javascript function when clicked:

function getDOC() 
{
    var str=window.location.href;

    var n=str.match(/#admin/); 
    if (n) 
   {
    window.open("../doc/adm.pdf");
    }

    n = str.match(/#api_primer/);
    if (n) 
    {
    window.open("../doc/api_primr.pdf");
    }

    n = str.match(/#cmds/);
    if (n) 
    {
    window.open("../doc/cmds.pdf");
    }

    n = str.match(/#install_config/);
    if (n) 
    {
    window.open("../doc/conf.pdf");
     }
}

Open in new window


But for some reason, the folder path is incorrect on execution... And the result is a file not found error:

"HTTP Status 404 - /doc/cmds.pdf"

It should be looking for ../docs/cmds.pdf.

Any ideas?
0
Comment
Question by:sconnell
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39766573
Is .. going up and outside your root folder?
0
 
LVL 4

Author Comment

by:sconnell
ID: 39766692
Not sure what you mean. It should find the documents in the relative path but the code ignores the ".." and assumes an absolute path.
0
 
LVL 58

Accepted Solution

by:
Gary earned 125 total points
ID: 39766704
Is doc within your root folder - the only reason I can think it is giving what you think is an absolute path is because you are trying go up outside the root of the site. In this case it would ignore the .. bit
Do you have a link to the page?
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39766707
For your js to work, you would need this directory structure where the javascript came from the file 'this.html'.
home
	html (web root)
		/docs
			cmds.pdf
		/this
			this.html

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39766710
Go on Dave, give a bad example.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39766755
What's wrong with it, Gary?
0
 
LVL 58

Expert Comment

by:Gary
ID: 39766771
I meant post a visual representation of where it would fail by trying to go outside the root (not that your example was wrong)
0
 
LVL 4

Author Comment

by:sconnell
ID: 39766779
I see what you guys are trying to tell me. This is what I was missing.

Ty
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39766801
I created a little demo that corresponds to the structure above.  In this, both the 'Direct' link and the 'Javascript' link should bring up the same document.  The javascript link opens in a new tab in Firefox.  I slightly modified your javascript for demo purposes.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Javascript link test</title>
<script type="text/javascript">
<!--
function getDOC(str) 
{
    //var str=window.location.href;

    var n=str.match(/#admin/); 
    if (n) 
   {
    window.open("../doc/adm.pdf");
    }

    n = str.match(/#api_primer/);
    if (n) 
    {
    window.open("../doc/api_primr.pdf");
    }

    n = str.match(/#cmds/);
    if (n) 
    {
    window.open("../doc/cmds.pdf");
    }

    n = str.match(/#install_config/);
    if (n) 
    {
    window.open("../doc/conf.pdf");
     }
}
// -->
</script>

</head>
<body>
<h1>Javascript link test</h1>
<h3>Direct links:</h3>
<a href="../doc/adm.pdf">../doc/adm.pdf</a><br />
<a href="../doc/api_primr.pdf">../doc/api_primr.pdf</a><br />
<a href="../doc/cmds.pdf">../doc/cmds.pdf</a><br />
<a href="../doc/conf.pdf">../doc/conf.pdf</a><br />

<h3>Javascript links:</h3>
<p onclick="getDOC('#admin');">../doc/adm.pdf</p>
<p onclick="getDOC('#api_primer');">../doc/api_primr.pdf</p>
<p onclick="getDOC('#cmds');">../doc/cmds.pdf</p>
<p onclick="getDOC('#install_config');">../doc/conf.pdf</p>

</body>
</html>

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 125 total points
ID: 39766815
Try this :
function getDOC()  {
    var hash = location.hash;
    var docs_path = location.pathname.split("/").slice(0,-2).join("/") + "/doc/";
    var filenames = { 
        "#admin":"adm.pdf",
        "#api_primer":"api_primr.pdf", 
        "#cmds":"cmds.pdf", 
        "#install_config":"conf.pdf"
    };
    if(filenames[hash]) {
        window.open( docs_path + filenames[hash] );
    };
}

Open in new window

0
 
LVL 4

Author Comment

by:sconnell
ID: 39766826
Awesome, I'll test this tonight. Much appreciated for your effort.
0
 
LVL 4

Author Comment

by:sconnell
ID: 39784043
Thank you again, everyone!
0

Featured Post

Save the day with this special offer from ATEN!

Save 30% on the CV211 using promo code EXPERTS30 now through April 30th. The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Accordion won't close correctly 5 14
Compute age Html 2 27
How to delete text box content on subform 1 22
allow link tag to be submit like form 14 23
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question