-4

I am trying to open a local html file from a running html file.In simple words, when I click on a button in my html page it should open another html page which is stored on my computer only. I have tried using href but it is not working.I am using onclick so when i click on the button it opens a new window but the html is not loaded on the page.I can only use html or Java Script.

Here is what I am doing

<html>
     <head>
      <script lang="Java-Script">
       function func()
       {
         var m='<a href="newone.html" target="-blank"></a>';
         document.write(m);
        }
       </script>
      </head>
      <body>
       <input type="Submit" value="click" onclick="func()">
     </body>
    </html>
bhakti123
  • 833
  • 10
  • 22
  • 2
    It is `target="_blank"`. What errors do you get? – putvande Mar 12 '14 at 13:45
  • 1
    Take a look at this answer: http://stackoverflow.com/a/18246357/1838483 – AfromanJ Mar 12 '14 at 13:46
  • 1
    To access local files you have to use `file://` URL. Your code looks for `newone.html` on the same server as the running html. – Barmar Mar 12 '14 at 13:47
  • You should avoid `document.write`, see warning in the [spec](http://www.w3.org/TR/html5/dom.html#document.write%28%29). And use ` – Oriol Mar 12 '14 at 13:48
  • I tried using `file://` but it is still not working.It just makes the whole screen blank.My `newone.html` is in the same folder as the running one.Do I need to provide the whole path? – bhakti123 Mar 12 '14 at 13:54

2 Answers2

2
  1. Forget about document.write. If you wanted to add a link element to a web page (not needed here), you could try using document.createElement and document.body.appendChild.
  2. If you want to navigate to an URL through Javascript, you can assign to window.location.
  3. Instead of a button, maybe you can make a normal link in the first place?
Kos
  • 70,399
  • 25
  • 169
  • 233
0

Use jquery lib. It is very easy. here you go!

http://fiddle.jshell.net/m7zq9/

Sergey Kudryashov
  • 713
  • 2
  • 9
  • 30