JavaScript Button cannot open another html file. Why?

Hello all,

I am trying to set up an onclick function in Javascript that opens another html file.
Below there are a few examples of the code used and the error messages they received.

Please note that it must be a button not a hyperlink and also it needs to be the genuine file that opens not a popup window.

I cannot fathom why a simple request to open a file with a button should be such a problem!

Can anyone assist please?

THE FOLLOWING CODE

<form>
<input type="button" name="open file!" value="open file!" onclick="openFile()">
</form>
function openFile()
{
	file.open(C:\\Documents and Settings\\Administrator\\My Documents\\abbtweb\	hetraininggame\\disclaimer.html)
}

GETS ERROR:-

  • line 145 Object expected (button line of code)

THE FOLLOWING CODE

<form>
<input type="button" name="open file!" value="open file!" onclick="openFile()">
</form>
function openFile()
{
	file.open(disclaimer.html)
}

GETS ERROR:-

  • line 227 file is undefined (file.open line of code)

THE FOLLOWING CODE

<form>
<input type="button" name="open file!" value="open file!" onclick="openFile()">
</form>
function openFile()
{
	file.open{C:\\Documents and Settings\\Administrator\\My Documents\\abbtweb\\disclaimer.html}
}

GETS ERRORS:-

  • line 227 char 11 expected ; (file.open line of code)
  • line 145 Object expected (button line of code)

THE FOLLOWING CODE

<form>
<input type="button" name="open file!" value="open file!" onclick="openFile()">
</form>
function openFile()
{
	window.open(disclaimer.html/)
}

GETS ERRORS:-

  • line 227 ‘disclaimer’ is undefined (window.open line of code)

THE FOLLOWING CODE

<form>
<input type="button" name="open file!" value="open file!" onclick="openFile()">
</form>
function openFile()
{
	window.open(/disclaimer.html/)
}

GETS ERRORS:-

  • line 227 Access Denied (window.open line of code)

Why? You are trying to mix HTTP actions with FileSystem actions.

You might be able to do this on your own OS by using VBscript eg.

<html><head>
<script language="vbscript">
set WSHshellObj = Createobject("Wscript.shell")
WSHshellObj.run "%windir%\
otepad.exe", 1, False
</script></head>
<body>
0 Hide the window and activate another window.<br />
1 Activate and display the window. (restore size and position) Specify this flag when displaying a window for the first time.<br />
2 Activate & minimize.<br />
3 Activate & maximize.<br />
4 Restore. The active window remains active.<br />
5 Activate & Restore.<br />
6 Minimize & activate the next top-level window in the Z order.<br />
7 Minimize. The active window remains active.<br />
8 Display the window in its current state. The active window remains active.<br />
9 Restore & Activate. Specify this flag when restoring a minimized window.<br />
10 Sets the show-state based on the state of the program that started the application.<br />
</body></html>

But doing this on a “live” site would be a major security risk and isn’t allowed. i.e. the “Access Denied”

The window.open would work if you surrounded the filename in quotes instead of slashes.

Hello Mittineague,

Thanks for reading my request.

You ask “why”.
It’s a requirement of my site - a file name is generated within the code from radio options checked in three separate arrays. Once all three are checked the user can then open the file by clicking a button.

If I can ask - why is this is such a security risk when hyperlinks (a href) are not?

I am new to all this (obviously) and I appreciate any info you can give me.

Thanks again.

I’m not sure I really understand what you want to do. If I’m right you want to navigate from one page to another just the way you do with a link, but using a button. I could ask why you want to use a button, but of course that’d be of no use to you. Remember, I’m giving you my answer based on what I understand your question to be.

The solution would be to use location.href

example:

myBtn.onclick = function(){
     window.location.href = "miPage.html"
}

Another solution would be to read the html document using PHP and the load the data via ajax, but that is just to much if what you want to do is what I think you want to do.

I hope I was of some help to you.:slight_smile:

Thanks Felgal,

That worked a treat with the example I gave you.:smiley:

This proves that I can open a file with a button, however, I am really using a constructed file name -

choosetopic()+''+chooseproject()+''+chooselod()+''+".html"

Replacing the “disclaimer.html” file name with the code above and placing quotes around it, as below, doesn’t work.

window.open("choosetopic()+''+chooseproject()+''+chooselod()+''+".html"")

I keep getting message - Expected ‘)’ character 72

Can you advise please?

try with:

window.open(choosetopic()+‘’+chooseproject()+‘’+chooselod()+‘’+“.html”);

Thanks ImBuzu

All suggestions are gratefully received.

You understood correctly - I’ll look at your method next.

Thanks again.

ImBuzu and Felgal,

Thankyou both very much the solution provided by ImBuzu worked.:smiley:

I’m a very satisfied bloke right now.

All the best.

No problem.

Now I’m confused. I remember that I while back Firefox had a bug where scripts could access a “chrome” file on the user’s computer and it was patched as a security fix. But it is possible for javascript to access a user’s filesystem?

Sounds scary to me.

It shouldn’t be. We are not accessing the file system. We are just requesting a file from the server the same way a file is requested with an anchor (<a />). Accessing the user’s file system is, just as you pointed out, a security issue, and we should not be allowed to do that. It sure sounds scarry:confused:.