-1

I want to link to a HTML file from a JavaScript on-click function. For that I used following function.

 onClick: function () {
                document.link("about.html");
               }

But this is not working. So how can I add link inside of onClick function. Can anyone help me.

Chathuri Fernando
  • 950
  • 3
  • 11
  • 22
  • 2
    `document.location = "about.html";` –  Jun 19 '18 at 10:02
  • 1
    Do you want to redirect to new page on click? If so, use window.location.href = "about.html"; – Alex S. Jun 19 '18 at 10:03
  • @ChrisG this work fine, If you can post this as an answer I can mark this as the correct answer. Thank you – Chathuri Fernando Jun 19 '18 at 10:05
  • I'm 100% sure this question is a duplicate. I can't find an older version right now though. –  Jun 19 '18 at 10:06
  • 1
    Possible duplicate of [Jumping to a new HTML page with JavaScript](https://stackoverflow.com/questions/442384/jumping-to-a-new-html-page-with-javascript) –  Jun 19 '18 at 10:07

4 Answers4

2

Usually I use this code:

<input type="button" value="Go to page" onclick="location.href='mypage.html'"/>

Anyway u can try:

onClick: function () {
                location.href("about.html");
               }
Save
  • 38
  • 4
1

You can use the onclick property of a button element in HTML that will trigger a function when the button is pressed.

<button onclick="myFunc()">click me</button>

In your JavaScript file, you can then create the myFunc() function that will be called and executed soon after.

function myFunc() {
    window.location = "otherpage.html";
}

According to W3C it's safer for cross-browser compatibility to use window.location rather than document.location.

0xbbadbeef
  • 34
  • 1
  • 3
0
onclick:function(){
 window.navigate('your_url');
}
Shadab
  • 15
  • 1
  • 6
  • 1
    While this code could solve the problem, it is best to add elaboration and explain how it works for people who might not understand this piece of code. – Papershine Jun 19 '18 at 13:03
0

Try this:-

 <input type="button" value="Go to page" onclick="openUrl()"/>

    function openUrl() {
              window.open("about.html");
    //or
    //window.location.href= "about.html";
        }
Mahi
  • 3,748
  • 4
  • 35
  • 70