?
Solved

Javascript issue (wrong path)

Posted on 2014-01-08
12
Medium Priority
?
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 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 84

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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 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 500 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

Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Progress

762 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