Solved

Javascript issue (wrong path)

Posted on 2014-01-08
12
254 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now