0

我试图在我的浏览器中打开一个文本文件,当从我的本地机器上打开它时它工作得很好但是当我将我的文件上传到我的网络服务器时,到文本文件的链接不再有效,我得到一个断开的链接错误浏览器。

这是我打开txt文件的代码:

<div style="width: 100%; background-color: #CCC;text-align: center;">
<a href="code.txt" onclick="window.open(this.href,this.target,'height=300, width=500');return false;">Code</a>
</div>
4

2 回答 2

0

A relative URL (neither a complete URL, i.e. one that starts with a protocol like http:// nor one that starts with a / indicating the root of the site) is relative to the last / in the URL. This means that if you have <a href="code.txt">Code</a> on http://www.example.com/dir/page.html then page.html is dropped and code.txt is appended, leading to http://www.example.com/dir/code.txt. If the code.txt file is not uploaded to that location on your webserver, it will not be found.

Are you sure code.txt is live on the server? That is to say can you navigate manually to the text file on your server?

Are you sure your PHP file is in the same directory as code.txt?

Are there any case (upper/lowercase) issues, like @BugFinder mentioned?

Does it work without the JavaScript popup? From your question it doesn't sound like a JS issue, but seeing as @Sathish thought there was something to correct with your popup behavior (it looks fine to me) it's worth removing that variable and seeing if a plain link works.

Are there any permission issues or restrictions on your server preventing the file from being displayed? For that matter, what is the exact error code and message you are receiving? 404? Something else?

I'm inclined to guess from what you've said so far that the file hasn't actually been uploaded. Alternatively, it's not actually in the same directory as your PHP file, despite what you've said. If it's neither of these issues, please update your question with more details.


If the file exists according to FTP, there's a couple of possibilities:

  1. The file is on the server, but not where you think it is. For example, many webhosts have a private root directory that is not accessible to the web, inside of which is a public_html/ or www/ directory which is the actual root of your website. Have you been able to upload other files to your website in the past? It sounds like the PHP file is successfully available, so assuming the txt file's in the same place, this isn't the issue.
  2. Your website is not set to allow .txt files to be served up to browsers. Have you been able to upload and see any txt files, or is this the first file with this problem? What happens if, for the sake of trying it, you rename the file to code.html and upload that to the same place?

For the record, this is likely a far better question to bring up with your host (or just look through their docs / support) than with StackOverflow, it's not a programming question but a hosting issue. Since we don't know who your host is, nor the intricacies of every individual hosting provider, it's hard to diagnose what's wrong.

于 2012-08-29T09:49:00.330 回答
0

要通过单击链接打开新窗口,您需要使用 javascript 的 window.open 方法。

<a href="javascript: void(0)" 
onclick="window.open('popup.txt', 
'windowname1', 
'width=200, height=77'); 
 return false;">Click here for txt</a>
于 2012-08-29T09:38:34.533 回答