Solved

Javascript issue (wrong path)

Posted on 2014-01-08
12
252 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
 
LVL 82

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 82

Expert Comment

by:Dave Baldwin
ID: 39766755
What's wrong with it, Gary?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 82

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

705 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

14 Experts available now in Live!

Get 1:1 Help Now