Solved

Javascript issue (wrong path)

Posted on 2014-01-08
12
253 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 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
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…
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)
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…

919 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

20 Experts available now in Live!

Get 1:1 Help Now